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