Home | History | Annotate | Download | only in menu
      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.support.annotation.Nullable;
     21 import android.text.TextUtils;
     22 
     23 import com.android.tv.MainActivity;
     24 import com.android.tv.R;
     25 import com.android.tv.customization.CustomAction;
     26 import com.android.tv.customization.TvCustomizationManager;
     27 
     28 import java.util.List;
     29 
     30 /**
     31  * A factory class to create menu rows.
     32  */
     33 public class MenuRowFactory {
     34     private final MainActivity mMainActivity;
     35     private final TvCustomizationManager mTvCustomizationManager;
     36 
     37     /**
     38      * A constructor.
     39      */
     40     public MenuRowFactory(MainActivity mainActivity) {
     41         mMainActivity = mainActivity;
     42         mTvCustomizationManager = new TvCustomizationManager(mainActivity);
     43         mTvCustomizationManager.initialize();
     44     }
     45 
     46     /**
     47      * Creates an object corresponding to the given {@code key}.
     48      */
     49     @Nullable
     50     public MenuRow createMenuRow(Menu menu, Class<?> key) {
     51         if (PlayControlsRow.class.equals(key)) {
     52             return new PlayControlsRow(mMainActivity, menu, mMainActivity.getTimeShiftManager());
     53         } else if (ChannelsRow.class.equals(key)) {
     54             return new ChannelsRow(mMainActivity, menu, mMainActivity.getProgramDataManager());
     55         } else if (PartnerRow.class.equals(key)) {
     56             List<CustomAction> customActions = mTvCustomizationManager.getCustomActions(
     57                     TvCustomizationManager.ID_PARTNER_ROW);
     58             String title = mTvCustomizationManager.getPartnerRowTitle();
     59             if (customActions != null && !TextUtils.isEmpty(title)) {
     60                 return new PartnerRow(mMainActivity, menu, title, customActions);
     61             }
     62             return null;
     63         } else if (TvOptionsRow.class.equals(key)) {
     64             return new TvOptionsRow(mMainActivity, menu, mTvCustomizationManager
     65                     .getCustomActions(TvCustomizationManager.ID_OPTIONS_ROW));
     66         } else if (PipOptionsRow.class.equals(key)) {
     67             return new PipOptionsRow(mMainActivity, menu);
     68         }
     69         return null;
     70     }
     71 
     72     /**
     73      * A menu row which represents the TV options row.
     74      */
     75     public static class TvOptionsRow extends ItemListRow {
     76         private TvOptionsRow(Context context, Menu menu, List<CustomAction> customActions) {
     77             super(context, menu, R.string.menu_title_options, R.dimen.action_card_height,
     78                     new TvOptionsRowAdapter(context, customActions));
     79         }
     80 
     81         @Override
     82         public void onStreamInfoChanged() {
     83             if (getMenu().isActive()) {
     84                 update();
     85             }
     86         }
     87     }
     88 
     89     /**
     90      * A menu row which represents the PIP options row.
     91      */
     92     public static class PipOptionsRow extends ItemListRow {
     93         private final MainActivity mMainActivity;
     94 
     95         private PipOptionsRow(Context context, Menu menu) {
     96             super(context, menu, R.string.menu_title_pip_options, R.dimen.action_card_height,
     97                     new PipOptionsRowAdapter(context));
     98             mMainActivity = (MainActivity) context;
     99         }
    100 
    101         @Override
    102         public boolean isVisible() {
    103             // TODO: Remove the dependency on MainActivity.
    104             return super.isVisible() && mMainActivity.isPipEnabled();
    105         }
    106     }
    107 
    108     /**
    109      * A menu row which represents the partner row.
    110      */
    111     public static class PartnerRow extends ItemListRow {
    112         private PartnerRow(Context context, Menu menu, String title,
    113                            List<CustomAction> customActions) {
    114             super(context, menu, title, R.dimen.action_card_height,
    115                     new PartnerOptionsRowAdapter(context, customActions));
    116         }
    117     }
    118 }
    119