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.media.tv.TvTrackInfo; 21 import android.support.annotation.VisibleForTesting; 22 23 import com.android.tv.Features; 24 import com.android.tv.TvOptionsManager; 25 import com.android.tv.customization.CustomAction; 26 import com.android.tv.data.DisplayMode; 27 import com.android.tv.ui.TvViewUiManager; 28 import com.android.tv.ui.sidepanel.ClosedCaptionFragment; 29 import com.android.tv.ui.sidepanel.DeveloperOptionFragment; 30 import com.android.tv.ui.sidepanel.DisplayModeFragment; 31 import com.android.tv.ui.sidepanel.MultiAudioFragment; 32 import com.android.tv.util.Utils; 33 34 import java.util.ArrayList; 35 import java.util.List; 36 37 /* 38 * An adapter of options. 39 */ 40 public class TvOptionsRowAdapter extends CustomizableOptionsRowAdapter { TvOptionsRowAdapter(Context context, List<CustomAction> customActions)41 public TvOptionsRowAdapter(Context context, List<CustomAction> customActions) { 42 super(context, customActions); 43 } 44 45 @Override createBaseActions()46 protected List<MenuAction> createBaseActions() { 47 List<MenuAction> actionList = new ArrayList<>(); 48 actionList.add(MenuAction.SELECT_CLOSED_CAPTION_ACTION); 49 actionList.add(MenuAction.SELECT_DISPLAY_MODE_ACTION); 50 if (Features.PICTURE_IN_PICTURE.isEnabled(getMainActivity())) { 51 actionList.add(MenuAction.SYSTEMWIDE_PIP_ACTION); 52 } 53 actionList.add(MenuAction.SELECT_AUDIO_LANGUAGE_ACTION); 54 actionList.add(MenuAction.MORE_CHANNELS_ACTION); 55 if (Utils.isDeveloper()) { 56 actionList.add(MenuAction.DEV_ACTION); 57 } 58 actionList.add(MenuAction.SETTINGS_ACTION); 59 60 updateClosedCaptionAction(); 61 updatePipAction(); 62 updateMultiAudioAction(); 63 updateDisplayModeAction(); 64 return actionList; 65 } 66 67 @Override updateActions()68 protected void updateActions() { 69 if (updateClosedCaptionAction()) { 70 notifyItemChanged(getItemPosition(MenuAction.SELECT_CLOSED_CAPTION_ACTION)); 71 } 72 if (updatePipAction()) { 73 notifyItemChanged(getItemPosition(MenuAction.SYSTEMWIDE_PIP_ACTION)); 74 } 75 if (updateMultiAudioAction()) { 76 notifyItemChanged(getItemPosition(MenuAction.SELECT_AUDIO_LANGUAGE_ACTION)); 77 } 78 if (updateDisplayModeAction()) { 79 notifyItemChanged(getItemPosition(MenuAction.SELECT_DISPLAY_MODE_ACTION)); 80 } 81 } 82 83 @VisibleForTesting updateClosedCaptionAction()84 private boolean updateClosedCaptionAction() { 85 return updateActionDescription(MenuAction.SELECT_CLOSED_CAPTION_ACTION); 86 } 87 updatePipAction()88 private boolean updatePipAction() { 89 if (containsItem(MenuAction.SYSTEMWIDE_PIP_ACTION)) { 90 return MenuAction.setEnabled(MenuAction.SYSTEMWIDE_PIP_ACTION, 91 !getMainActivity().isScreenBlockedByResourceConflictOrParentalControl()); 92 } 93 return false; 94 } 95 updateMultiAudioAction()96 boolean updateMultiAudioAction() { 97 List<TvTrackInfo> audioTracks = getMainActivity().getTracks(TvTrackInfo.TYPE_AUDIO); 98 boolean enabled = audioTracks != null && audioTracks.size() > 1; 99 // Use "|" operator for non-short-circuit evaluation. 100 return MenuAction.setEnabled(MenuAction.SELECT_AUDIO_LANGUAGE_ACTION, enabled) 101 | updateActionDescription(MenuAction.SELECT_AUDIO_LANGUAGE_ACTION); 102 } 103 updateDisplayModeAction()104 private boolean updateDisplayModeAction() { 105 TvViewUiManager uiManager = getMainActivity().getTvViewUiManager(); 106 boolean enabled = uiManager.isDisplayModeAvailable(DisplayMode.MODE_FULL) 107 || uiManager.isDisplayModeAvailable(DisplayMode.MODE_ZOOM); 108 // Use "|" operator for non-short-circuit evaluation. 109 return MenuAction.setEnabled(MenuAction.SELECT_DISPLAY_MODE_ACTION, enabled) 110 | updateActionDescription(MenuAction.SELECT_DISPLAY_MODE_ACTION); 111 } 112 updateActionDescription(MenuAction action)113 private boolean updateActionDescription(MenuAction action) { 114 return MenuAction.setActionDescription(action, 115 getMainActivity().getTvOptionsManager().getOptionString(action.getType())); 116 } 117 118 @Override executeBaseAction(int type)119 protected void executeBaseAction(int type) { 120 switch (type) { 121 case TvOptionsManager.OPTION_CLOSED_CAPTIONS: 122 getMainActivity().getOverlayManager().getSideFragmentManager() 123 .show(new ClosedCaptionFragment()); 124 break; 125 case TvOptionsManager.OPTION_DISPLAY_MODE: 126 getMainActivity().getOverlayManager().getSideFragmentManager() 127 .show(new DisplayModeFragment()); 128 break; 129 case TvOptionsManager.OPTION_SYSTEMWIDE_PIP: 130 getMainActivity().enterPictureInPictureMode(); 131 break; 132 case TvOptionsManager.OPTION_MULTI_AUDIO: 133 getMainActivity().getOverlayManager().getSideFragmentManager() 134 .show(new MultiAudioFragment()); 135 break; 136 case TvOptionsManager.OPTION_MORE_CHANNELS: 137 getMainActivity().showMerchantCollection(); 138 break; 139 case TvOptionsManager.OPTION_DEVELOPER: 140 getMainActivity().getOverlayManager().getSideFragmentManager() 141 .show(new DeveloperOptionFragment()); 142 break; 143 case TvOptionsManager.OPTION_SETTINGS: 144 getMainActivity().showSettingsFragment(); 145 break; 146 } 147 } 148 }