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.ui.sidepanel.parentalcontrols; 18 19 import android.view.View; 20 import android.widget.TextView; 21 import com.android.tv.MainActivity; 22 import com.android.tv.R; 23 import com.android.tv.data.api.Channel; 24 import com.android.tv.dialog.PinDialogFragment; 25 import com.android.tv.ui.sidepanel.ActionItem; 26 import com.android.tv.ui.sidepanel.Item; 27 import com.android.tv.ui.sidepanel.SideFragment; 28 import com.android.tv.ui.sidepanel.SubMenuItem; 29 import com.android.tv.ui.sidepanel.SwitchItem; 30 import java.util.ArrayList; 31 import java.util.List; 32 33 public class ParentalControlsFragment extends SideFragment { 34 private static final String TRACKER_LABEL = "Parental controls"; 35 private List<ActionItem> mActionItems; 36 37 private final SideFragmentListener mSideFragmentListener = 38 new SideFragmentListener() { 39 @Override 40 public void onSideFragmentViewDestroyed() { 41 notifyDataSetChanged(); 42 } 43 }; 44 45 @Override getTitle()46 protected String getTitle() { 47 return getString(R.string.menu_parental_controls); 48 } 49 50 @Override getTrackerLabel()51 public String getTrackerLabel() { 52 return TRACKER_LABEL; 53 } 54 55 @Override getItemList()56 protected List<Item> getItemList() { 57 List<Item> items = new ArrayList<>(); 58 items.add( 59 new SwitchItem( 60 getString(R.string.option_toggle_parental_controls_on), 61 getString(R.string.option_toggle_parental_controls_off)) { 62 @Override 63 protected void onUpdate() { 64 super.onUpdate(); 65 setChecked( 66 getMainActivity() 67 .getParentalControlSettings() 68 .isParentalControlsEnabled()); 69 } 70 71 @Override 72 protected void onSelected() { 73 super.onSelected(); 74 boolean checked = isChecked(); 75 getMainActivity() 76 .getParentalControlSettings() 77 .setParentalControlsEnabled(checked); 78 enableActionItems(checked); 79 } 80 }); 81 82 mActionItems = new ArrayList<>(); 83 mActionItems.add( 84 new SubMenuItem( 85 getString(R.string.option_channels_locked), 86 "", 87 getMainActivity().getOverlayManager().getSideFragmentManager()) { 88 TextView mDescriptionView; 89 90 @Override 91 protected SideFragment getFragment() { 92 SideFragment fragment = new ChannelsBlockedFragment(); 93 fragment.setListener(mSideFragmentListener); 94 return fragment; 95 } 96 97 @Override 98 protected void onBind(View view) { 99 super.onBind(view); 100 mDescriptionView = (TextView) view.findViewById(R.id.description); 101 } 102 103 @Override 104 protected void onUpdate() { 105 super.onUpdate(); 106 int lockedAndBrowsableChannelCount = 0; 107 for (Channel channel : getChannelDataManager().getChannelList()) { 108 if (channel.isLocked() && channel.isBrowsable()) { 109 ++lockedAndBrowsableChannelCount; 110 } 111 } 112 if (lockedAndBrowsableChannelCount > 0) { 113 mDescriptionView.setText( 114 Integer.toString(lockedAndBrowsableChannelCount)); 115 } else { 116 mDescriptionView.setText( 117 getMainActivity().getString(R.string.option_no_locked_channel)); 118 } 119 } 120 121 @Override 122 protected void onUnbind() { 123 super.onUnbind(); 124 mDescriptionView = null; 125 } 126 }); 127 mActionItems.add( 128 new SubMenuItem( 129 getString(R.string.option_program_restrictions), 130 ProgramRestrictionsFragment.getDescription(getMainActivity()), 131 getMainActivity().getOverlayManager().getSideFragmentManager()) { 132 @Override 133 protected SideFragment getFragment() { 134 SideFragment fragment = new ProgramRestrictionsFragment(); 135 fragment.setListener(mSideFragmentListener); 136 return fragment; 137 } 138 }); 139 mActionItems.add( 140 new ActionItem(getString(R.string.option_change_pin)) { 141 @Override 142 protected void onSelected() { 143 final MainActivity tvActivity = getMainActivity(); 144 tvActivity.getOverlayManager().getSideFragmentManager().hideSidePanel(true); 145 PinDialogFragment fragment = 146 PinDialogFragment.create(PinDialogFragment.PIN_DIALOG_TYPE_NEW_PIN); 147 tvActivity 148 .getOverlayManager() 149 .showDialogFragment(PinDialogFragment.DIALOG_TAG, fragment, true); 150 } 151 }); 152 items.addAll(mActionItems); 153 enableActionItems( 154 getMainActivity().getParentalControlSettings().isParentalControlsEnabled()); 155 return items; 156 } 157 enableActionItems(boolean enabled)158 private void enableActionItems(boolean enabled) { 159 for (ActionItem actionItem : mActionItems) { 160 actionItem.setEnabled(enabled); 161 } 162 } 163 } 164