1/*
2 * Copyright (C) 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
17package com.android.tv.ui.sidepanel;
18
19import android.app.Activity;
20
21import com.android.tv.R;
22import com.android.tv.data.DisplayMode;
23import com.android.tv.ui.TvViewUiManager;
24
25import java.util.ArrayList;
26import java.util.List;
27
28public class DisplayModeFragment extends SideFragment {
29    private static final String TRACKER_LABEL = "display mode";
30    private TvViewUiManager mTvViewUiManager;
31
32    @Override
33    protected String getTitle() {
34        return getString(R.string.side_panel_title_display_mode);
35    }
36
37    @Override
38    public String getTrackerLabel() {
39        return TRACKER_LABEL;
40    }
41
42    @Override
43    protected List<Item> getItemList() {
44        List<Item> items = new ArrayList<>();
45        for (int i = 0; i < DisplayMode.SIZE_OF_RATIO_TYPES; ++i) {
46            items.add(new DisplayModeRadioItem(i));
47        }
48        return items;
49    }
50
51    @Override
52    public void onAttach(Activity activity) {
53        super.onAttach(activity);
54        mTvViewUiManager = getMainActivity().getTvViewUiManager();
55    }
56
57    @Override
58    public void onResume() {
59        super.onResume();
60        setSelectedPosition(mTvViewUiManager.getDisplayMode());
61    }
62
63    @Override
64    public void onDetach() {
65        super.onDetach();
66        mTvViewUiManager.restoreDisplayMode(true);
67    }
68
69    private class DisplayModeRadioItem extends RadioButtonItem {
70        private final int mDisplayMode;
71
72        private DisplayModeRadioItem(int displayMode) {
73            super(DisplayMode.getLabel(displayMode, getActivity()));
74            mDisplayMode = displayMode;
75        }
76
77        @Override
78        protected void onUpdate() {
79            super.onUpdate();
80            setEnabled(mTvViewUiManager.isDisplayModeAvailable(mDisplayMode));
81            setChecked(mDisplayMode == mTvViewUiManager.getDisplayMode());
82        }
83
84        @Override
85        protected void onSelected() {
86            super.onSelected();
87            mTvViewUiManager.setDisplayMode(mDisplayMode, true, true);
88            closeFragment();
89        }
90
91        @Override
92        protected void onFocused() {
93            super.onFocused();
94            mTvViewUiManager.setDisplayMode(mDisplayMode, false, true);
95        }
96    }
97}
98