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