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