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 
17 package com.android.tv.menu;
18 
19 import android.content.Context;
20 import android.text.TextUtils;
21 
22 import com.android.tv.MainActivity;
23 import com.android.tv.R;
24 import com.android.tv.TvOptionsManager;
25 import com.android.tv.ui.TvViewUiManager;
26 import com.android.tv.ui.sidepanel.PipInputSelectorFragment;
27 import com.android.tv.util.PipInputManager.PipInput;
28 import com.android.tv.util.TvSettings;
29 
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 /*
34  * An adapter of PIP options.
35  */
36 public class PipOptionsRowAdapter extends OptionsRowAdapter {
37     private static final int[] DRAWABLE_ID_FOR_LAYOUT = {
38             R.drawable.ic_pip_option_layout1,
39             R.drawable.ic_pip_option_layout2,
40             R.drawable.ic_pip_option_layout3,
41             R.drawable.ic_pip_option_layout4,
42             R.drawable.ic_pip_option_layout5 };
43 
44     private final TvOptionsManager mTvOptionsManager;
45     private final TvViewUiManager mTvViewUiManager;
46 
PipOptionsRowAdapter(Context context)47     public PipOptionsRowAdapter(Context context) {
48         super(context);
49         mTvOptionsManager = getMainActivity().getTvOptionsManager();
50         mTvViewUiManager = getMainActivity().getTvViewUiManager();
51     }
52 
53     @Override
createActions()54     protected List<MenuAction> createActions() {
55         List<MenuAction> actionList = new ArrayList<>();
56         actionList.add(MenuAction.PIP_SELECT_INPUT_ACTION);
57         actionList.add(MenuAction.PIP_SWAP_ACTION);
58         actionList.add(MenuAction.PIP_SOUND_ACTION);
59         actionList.add(MenuAction.PIP_LAYOUT_ACTION);
60         actionList.add(MenuAction.PIP_SIZE_ACTION);
61         for (MenuAction action : actionList) {
62             setOptionChangedListener(action);
63         }
64         return actionList;
65     }
66 
67     @Override
updateActions()68     public boolean updateActions() {
69         boolean changed = false;
70         if (updateSelectInputAction()) {
71             changed = true;
72         }
73         if (updateLayoutAction()) {
74             changed = true;
75         }
76         if (updateSizeAction()) {
77             changed = true;
78         }
79         return changed;
80     }
81 
updateSelectInputAction()82     private boolean updateSelectInputAction() {
83         String oldInputLabel = mTvOptionsManager.getOptionString(TvOptionsManager.OPTION_PIP_INPUT);
84 
85         MainActivity tvActivity = getMainActivity();
86         PipInput newInput = tvActivity.getPipInputManager().getPipInput(tvActivity.getPipChannel());
87         String newInputLabel = newInput == null ? null : newInput.getLabel();
88 
89         if (!TextUtils.equals(oldInputLabel, newInputLabel)) {
90             mTvOptionsManager.onPipInputChanged(newInputLabel);
91             return true;
92         }
93         return false;
94     }
95 
updateLayoutAction()96     private boolean updateLayoutAction() {
97         return MenuAction.PIP_LAYOUT_ACTION.setDrawableResId(
98             DRAWABLE_ID_FOR_LAYOUT[mTvViewUiManager.getPipLayout()]);
99     }
100 
updateSizeAction()101     private boolean updateSizeAction() {
102         boolean oldEnabled = MenuAction.PIP_SIZE_ACTION.isEnabled();
103         boolean newEnabled = mTvViewUiManager.getPipLayout() != TvSettings.PIP_LAYOUT_SIDE_BY_SIDE;
104         if (oldEnabled != newEnabled) {
105             MenuAction.PIP_SIZE_ACTION.setEnabled(newEnabled);
106             return true;
107         }
108         return false;
109     }
110 
111     @Override
executeAction(int type)112     protected void executeAction(int type) {
113         switch (type) {
114             case TvOptionsManager.OPTION_PIP_INPUT:
115                 getMainActivity().getOverlayManager().getSideFragmentManager().show(
116                         new PipInputSelectorFragment());
117                 break;
118             case TvOptionsManager.OPTION_PIP_SWAP:
119                 getMainActivity().swapPip();
120                 break;
121             case TvOptionsManager.OPTION_PIP_SOUND:
122                 getMainActivity().togglePipSoundMode();
123                 break;
124             case TvOptionsManager.OPTION_PIP_LAYOUT:
125                 int oldLayout = mTvViewUiManager.getPipLayout();
126                 int newLayout = (oldLayout + 1) % (TvSettings.PIP_LAYOUT_LAST + 1);
127                 mTvViewUiManager.setPipLayout(newLayout, true);
128                 MenuAction.PIP_LAYOUT_ACTION.setDrawableResId(DRAWABLE_ID_FOR_LAYOUT[newLayout]);
129                 break;
130             case TvOptionsManager.OPTION_PIP_SIZE:
131                 int oldSize = mTvViewUiManager.getPipSize();
132                 int newSize = (oldSize + 1) % (TvSettings.PIP_SIZE_LAST + 1);
133                 mTvViewUiManager.setPipSize(newSize, true);
134                 break;
135         }
136     }
137 }
138