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
17package com.android.tv.ui.sidepanel.parentalcontrols;
18
19import android.graphics.drawable.Drawable;
20import android.view.View;
21import android.widget.CompoundButton;
22import android.widget.ImageView;
23
24import com.android.tv.R;
25import com.android.tv.parental.ContentRatingSystem;
26import com.android.tv.parental.ContentRatingSystem.Rating;
27import com.android.tv.parental.ContentRatingSystem.SubRating;
28import com.android.tv.ui.sidepanel.CheckBoxItem;
29import com.android.tv.ui.sidepanel.DividerItem;
30import com.android.tv.ui.sidepanel.Item;
31import com.android.tv.ui.sidepanel.SideFragment;
32
33import java.util.ArrayList;
34import java.util.List;
35
36public class SubRatingsFragment extends SideFragment {
37    private static final String TRACKER_LABEL = "Sub ratings";
38
39    private final ContentRatingSystem mContentRatingSystem;
40    private final Rating mRating;
41    private final List<SubRatingItem> mSubRatingItems = new ArrayList<>();
42
43    public SubRatingsFragment(ContentRatingSystem contentRatingSystem, Rating rating) {
44        mContentRatingSystem = contentRatingSystem;
45        mRating = rating;
46    }
47
48    @Override
49    protected String getTitle() {
50        return getString(R.string.option_subrating_title, mRating.getTitle());
51    }
52
53    @Override
54    public String getTrackerLabel() {
55        return TRACKER_LABEL;
56    }
57
58    @Override
59    protected List<Item> getItemList() {
60        List<Item> items = new ArrayList<>();
61        items.add(new RatingItem());
62        items.add(new DividerItem(getString(R.string.option_subrating_header)));
63        mSubRatingItems.clear();
64        for (SubRating subRating : mRating.getSubRatings()) {
65            mSubRatingItems.add(new SubRatingItem(subRating));
66        }
67        items.addAll(mSubRatingItems);
68        return items;
69    }
70
71    private class RatingItem extends CheckBoxItem {
72        private RatingItem() {
73            super(mRating.getTitle(), mRating.getDescription());
74        }
75
76        @Override
77        protected void onBind(View view) {
78            super.onBind(view);
79
80            CompoundButton button = (CompoundButton) view.findViewById(getCompoundButtonId());
81            button.setButtonDrawable(R.drawable.btn_lock_material_anim);
82            button.setVisibility(View.VISIBLE);
83
84            Drawable icon = mRating.getIcon();
85            ImageView imageView = (ImageView) view.findViewById(R.id.icon);
86            if (icon != null) {
87                imageView.setVisibility(View.VISIBLE);
88                imageView.setImageDrawable(icon);
89            } else {
90                imageView.setVisibility(View.GONE);
91            }
92        }
93
94        @Override
95        protected void onUpdate() {
96            super.onUpdate();
97            setChecked(isRatingEnabled());
98        }
99
100        @Override
101        protected void onSelected() {
102            super.onSelected();
103            boolean checked = isChecked();
104            setRatingEnabled(checked);
105            if (checked) {
106                // If the rating is checked, check and disable all the sub rating items.
107                for (SubRating subRating : mRating.getSubRatings()) {
108                    setSubRatingEnabled(subRating, true);
109                }
110                for (SubRatingItem item : mSubRatingItems) {
111                    item.setChecked(true);
112                    item.setEnabled(false);
113                }
114            } else {
115                // If the rating is unchecked, just enable all the sub rating items and do not
116                // change the check state.
117                for (SubRatingItem item : mSubRatingItems) {
118                    item.setEnabled(true);
119                }
120            }
121        }
122
123        @Override
124        protected int getResourceId() {
125            return R.layout.option_item_rating;
126        }
127    }
128
129    private class SubRatingItem extends CheckBoxItem {
130        private final SubRating mSubRating;
131
132        private SubRatingItem(SubRating subRating) {
133            super(subRating.getTitle(), subRating.getDescription());
134            mSubRating = subRating;
135        }
136
137        @Override
138        protected void onBind(View view) {
139            super.onBind(view);
140
141            CompoundButton button = (CompoundButton) view.findViewById(getCompoundButtonId());
142            button.setButtonDrawable(R.drawable.btn_lock_material_anim);
143            button.setVisibility(View.VISIBLE);
144
145            Drawable icon = mSubRating.getIcon();
146            ImageView imageView = (ImageView) view.findViewById(R.id.icon);
147            if (icon != null) {
148                imageView.setVisibility(View.VISIBLE);
149                imageView.setImageDrawable(icon);
150            } else {
151                imageView.setVisibility(View.GONE);
152            }
153        }
154
155        @Override
156        protected void onUpdate() {
157            super.onUpdate();
158            setChecked(isSubRatingEnabled(mSubRating));
159            setEnabled(!isRatingEnabled());
160        }
161
162        @Override
163        protected void onSelected() {
164            super.onSelected();
165            setSubRatingEnabled(mSubRating, isChecked());
166        }
167
168        @Override
169        protected int getResourceId() {
170            return R.layout.option_item_rating;
171        }
172    }
173
174    private boolean isRatingEnabled() {
175        return getMainActivity().getParentalControlSettings()
176                .isRatingBlocked(mContentRatingSystem, mRating);
177    }
178
179    private boolean isSubRatingEnabled(SubRating subRating) {
180        return getMainActivity().getParentalControlSettings()
181                .isSubRatingEnabled(mContentRatingSystem, mRating, subRating);
182    }
183
184    private void setRatingEnabled(boolean enabled) {
185        getMainActivity().getParentalControlSettings()
186                .setRatingBlocked(mContentRatingSystem, mRating, enabled);
187    }
188
189    private void setSubRatingEnabled(SubRating subRating, boolean enabled) {
190        getMainActivity().getParentalControlSettings()
191                .setSubRatingBlocked(mContentRatingSystem, mRating, subRating, enabled);
192    }
193}
194