1 /*
2  * Copyright 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.sampletvinput.rich;
18 
19 import android.graphics.Color;
20 import android.media.tv.TvInputInfo;
21 import android.os.Bundle;
22 import android.support.v17.leanback.app.BrowseFragment;
23 import android.support.v17.leanback.widget.ArrayObjectAdapter;
24 import android.support.v17.leanback.widget.HeaderItem;
25 import android.support.v17.leanback.widget.ListRow;
26 import android.support.v17.leanback.widget.ListRowPresenter;
27 import android.support.v17.leanback.widget.OnItemViewClickedListener;
28 import android.support.v17.leanback.widget.OnItemViewSelectedListener;
29 import android.support.v17.leanback.widget.Presenter;
30 import android.support.v17.leanback.widget.Row;
31 import android.support.v17.leanback.widget.RowPresenter;
32 import android.text.TextUtils;
33 import android.view.Gravity;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.widget.TextView;
37 import android.widget.Toast;
38 
39 import com.example.android.sampletvinput.R;
40 import com.example.android.sampletvinput.TvContractUtils;
41 
42 /**
43  * Fragment which shows a sample UI for configuring {@link RichTvInputService}.
44  */
45 public class RichSettingsFragment extends BrowseFragment {
46     private static final String TAG = "SettingsFragment";
47 
48     private static final int GRID_ITEM_WIDTH = 500;
49     private static final int GRID_ITEM_HEIGHT = 200;
50 
51     private ArrayObjectAdapter mRowsAdapter;
52 
53     // Container Activity must implement this interface
54     public interface SettingsClickedListener {
onSettingsClicked()55         public void onSettingsClicked();
56     }
57 
58     @Override
onActivityCreated(Bundle savedInstanceState)59     public void onActivityCreated(Bundle savedInstanceState) {
60         super.onActivityCreated(savedInstanceState);
61 
62         setupUIElements();
63 
64         loadRows();
65 
66         setupEventListeners();
67     }
68 
69     @Override
onDestroy()70     public void onDestroy() {
71         super.onDestroy();
72     }
73 
loadRows()74     private void loadRows() {
75 
76         String inputId = getActivity().getIntent().getStringExtra(TvInputInfo.EXTRA_INPUT_ID);
77         String serviceName = TvContractUtils.getServiceNameFromInputId(getActivity(), inputId);
78 
79         if (!TextUtils.isEmpty(serviceName)) {
80             String[] a = serviceName.split("\\.");
81             serviceName = a[a.length - 1];
82         }
83 
84         GridItemPresenter mGridPresenter = new GridItemPresenter();
85 
86         mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
87         HeaderItem gridHeader = new HeaderItem(0, "SETTINGS", null);
88 
89         ArrayObjectAdapter gridRowAdapter = new ArrayObjectAdapter(mGridPresenter);
90         gridRowAdapter.add(serviceName + " Settings");
91         if (RichTvInputService.class.getName().contains(serviceName) ) {
92             gridRowAdapter.add("Server URLs");
93             gridRowAdapter.add("Update Frequency");
94         }
95         mRowsAdapter.add(new ListRow(gridHeader, gridRowAdapter));
96 
97         setAdapter(mRowsAdapter);
98     }
99 
setupUIElements()100     private void setupUIElements() {
101         setTitle(getString(R.string.rich_settings_browse_title));
102         setHeadersState(HEADERS_DISABLED);
103         setHeadersTransitionOnBackEnabled(true);
104 
105         // set fastLane (or headers) background color
106         setBrandColor(getResources().getColor(R.color.fastlane_background));
107         // set search icon color
108         setSearchAffordanceColor(getResources().getColor(R.color.search_opaque));
109     }
110 
setupEventListeners()111     private void setupEventListeners() {
112         setOnItemViewClickedListener(new ItemViewClickedListener());
113         setOnItemViewSelectedListener(new ItemViewSelectedListener());
114     }
115 
116     private final class ItemViewClickedListener implements OnItemViewClickedListener {
117         @Override
onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row)118         public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
119                                   RowPresenter.ViewHolder rowViewHolder, Row row) {
120 
121             if (item instanceof String) {
122                 Toast.makeText(getActivity(), ((String) item), Toast.LENGTH_SHORT).show();
123             }
124         }
125     }
126 
127 
128     private final class ItemViewSelectedListener implements OnItemViewSelectedListener {
129         @Override
onItemSelected(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row)130         public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
131                                    RowPresenter.ViewHolder rowViewHolder, Row row) {
132         }
133     }
134 
135     private class GridItemPresenter extends Presenter {
136         @Override
onCreateViewHolder(ViewGroup parent)137         public ViewHolder onCreateViewHolder(ViewGroup parent) {
138             TextView view = new TextView(parent.getContext());
139             view.setLayoutParams(new ViewGroup.LayoutParams(GRID_ITEM_WIDTH, GRID_ITEM_HEIGHT));
140             view.setFocusable(true);
141             view.setFocusableInTouchMode(true);
142             view.setBackgroundColor(getResources().getColor(R.color.default_background));
143             view.setTextColor(Color.WHITE);
144             view.setGravity(Gravity.CENTER);
145             return new ViewHolder(view);
146         }
147 
148         @Override
onBindViewHolder(ViewHolder viewHolder, Object item)149         public void onBindViewHolder(ViewHolder viewHolder, Object item) {
150             ((TextView) viewHolder.view).setText((String) item);
151         }
152 
153         @Override
onUnbindViewHolder(ViewHolder viewHolder)154         public void onUnbindViewHolder(ViewHolder viewHolder) {
155         }
156     }
157 
158 }
159