aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/ui/BaseSideFragment.java
blob: 090fcd36c36a971054f0b8e0f01f147bd5bac119 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.ui;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.support.v17.leanback.widget.VerticalGridView;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.internal.util.Preconditions;
import com.android.tv.R;

public class BaseSideFragment extends Fragment {
    private static final String TAG = "BaseSideFragment";

    public static final String KEY_INITIATOR = "last_state";
    public static final int INITIATOR_UNKNOWN = 0;
    public static final int INITIATOR_SHORTCUT_KEY = 1;
    public static final int INITIATOR_MENU = 2;

    private String mTitle;
    private TextView mTitleView;
    private VerticalGridView mOptionItemListView;
    private LayoutInflater mLayoutInflater;
    private Object[] mItemTags;
    private int mPrevSelectedItemPosition;
    private int mFragmentLayoutId;
    private int mItemLayoutId;
    private int mInitiator;
    private Handler mHandler = new Handler();
    private int mItemBgColor;
    private int mItemFocusedBgColor;
    private int mItemHeight;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // initialize should be called before onCreateView.
        Preconditions.checkState(!TextUtils.isEmpty(mTitle));
        Bundle arg = getArguments();
        if (arg != null && arg.containsKey(KEY_INITIATOR)) {
            mInitiator = arg.getInt(KEY_INITIATOR);
        } else {
            mInitiator = INITIATOR_UNKNOWN;
        }

        View fragView = inflater.inflate(mFragmentLayoutId, container, false);
        mTitleView = (TextView) fragView.findViewById(R.id.side_panel_title);
        mTitleView.setText(mTitle);
        mOptionItemListView = (VerticalGridView) fragView.findViewById(R.id.side_panel_list);
        mOptionItemListView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom,
                    int oldLeft, int oldTop, int oldRight, int oldBottom) {
                int padding = ((v.getHeight() + mItemHeight) / 2) % mItemHeight + mItemHeight;
                v.setPadding(v.getPaddingLeft(), v.getPaddingTop(), v.getPaddingRight(), padding);
            }
        });

        mOptionItemListView.setAdapter(new OptionItemAdapter());
        mLayoutInflater = inflater;
        return fragView;
    }

    public int getInitiator() {
        return mInitiator;
    }

    public void notifyDataSetChanged() {
        if (mOptionItemListView != null) {
            mOptionItemListView.getAdapter().notifyDataSetChanged();
        }
    }

    public void setPrevSelectedItemPosition(int position) {
        mPrevSelectedItemPosition = position;
        notifyDataSetChanged();
    }

    public void onItemFocusChanged(View v, boolean focusGained, int position, Object tag) {
        v.setBackgroundColor(focusGained ? mItemFocusedBgColor : mItemBgColor);
    }

    public void onItemSelected(View v, int position, Object tag) {
    }

    public void onCreatedChildView(View v) {
    }

    public void onBindView(View v, int position, Object tag, boolean prevSelected) {
    }

    public void setSelectedPosition(int position) {
        mOptionItemListView.setSelectedPosition(position);
        mOptionItemListView.requestFocus();
    }

    protected void initialize(String title, Object[] itemTags, int fragmentLayoutId,
            int itemLayoutId, int itemBgColorResId, int itemFocusedBgColorResId,
            int itemHeightResId) {
        Preconditions.checkState(!TextUtils.isEmpty(title));
        mTitle = title;
        mItemTags = itemTags;
        mFragmentLayoutId = fragmentLayoutId;
        mItemLayoutId = itemLayoutId;
        mItemBgColor = getActivity().getResources().getColor(itemBgColorResId);
        mItemFocusedBgColor = getActivity().getResources().getColor(itemFocusedBgColorResId);
        mItemHeight = getActivity().getResources().getDimensionPixelOffset(itemHeightResId);
        notifyDataSetChanged();
    }

    class OptionItemAdapter extends RecyclerView.Adapter<OptionItemAdapter.MyViewHolder> {
        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = mLayoutInflater.inflate(mItemLayoutId, parent, false);
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = (Integer) v.getTag(R.id.TAG_OPTION_ITEM_POSITOIN);
                    onItemSelected(v, position, mItemTags[position]);
                }
            });
            v.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean focusGained) {
                    int position = (Integer) v.getTag(R.id.TAG_OPTION_ITEM_POSITOIN);
                    onItemFocusChanged(v, focusGained, position, mItemTags[position]);
                }
            });
            onCreatedChildView(v);
            return new MyViewHolder(v);
        }

        @Override
        public void onBindViewHolder(MyViewHolder baseHolder, int position) {
            View v = baseHolder.itemView;
            v.setVisibility(View.VISIBLE);
            onBindView(v, position, mItemTags[position], position == mPrevSelectedItemPosition);
            v.setTag(R.id.TAG_OPTION_ITEM_POSITOIN, position);
            if (v instanceof ViewGroup) {
                final ViewGroup viewGroup = (ViewGroup) v;
                mHandler.post(new Runnable() {
                    void requestLayout(ViewGroup v) {
                        for (int i = 0; i < v.getChildCount(); i++) {
                            v.getChildAt(i).requestLayout();
                            if (v.getChildAt(i) instanceof ViewGroup) {
                                requestLayout((ViewGroup) v.getChildAt(i));
                            }
                        }
                    }

                    @Override
                    public void run() {
                        requestLayout(viewGroup);
                    }
                });
            }
        }

        @Override
        public int getItemCount() {
            return mItemTags == null ? 0 : mItemTags.length;
        }

        private class MyViewHolder extends RecyclerView.ViewHolder {
            MyViewHolder(View view) {
                super(view);
            }
        }
    }
}