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.os.Bundle; 20 import com.android.tv.MainActivity; 21 import com.android.tv.R; 22 import com.android.tv.parental.ContentRatingSystem; 23 import com.android.tv.parental.ContentRatingsManager; 24 import com.android.tv.parental.ParentalControlSettings; 25 import com.android.tv.ui.sidepanel.ActionItem; 26 import com.android.tv.ui.sidepanel.CheckBoxItem; 27 import com.android.tv.ui.sidepanel.Item; 28 import com.android.tv.ui.sidepanel.SideFragment; 29 import com.android.tv.util.TvSettings; 30 import java.util.ArrayList; 31 import java.util.Collections; 32 import java.util.List; 33 import java.util.Locale; 34 35 public class RatingSystemsFragment extends SideFragment { 36 private static final String TRACKER_LABEL = "Rating systems"; 37 38 @Override onCreate(Bundle savedInstanceState)39 public void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 setDefaultRatingSystemsIfNeeded((MainActivity) getActivity()); 42 } 43 getDescription(MainActivity tvActivity)44 public static String getDescription(MainActivity tvActivity) { 45 setDefaultRatingSystemsIfNeeded(tvActivity); 46 47 List<ContentRatingSystem> contentRatingSystems = 48 tvActivity.getContentRatingsManager().getContentRatingSystems(); 49 Collections.sort(contentRatingSystems, ContentRatingSystem.DISPLAY_NAME_COMPARATOR); 50 StringBuilder builder = new StringBuilder(); 51 for (ContentRatingSystem s : contentRatingSystems) { 52 if (!tvActivity.getParentalControlSettings().isContentRatingSystemEnabled(s)) { 53 continue; 54 } 55 builder.append(s.getDisplayName()); 56 builder.append(", "); 57 } 58 return builder.length() > 0 59 ? builder.substring(0, builder.length() - 2) 60 : tvActivity.getString(R.string.option_no_enabled_rating_system); 61 } 62 63 @Override getTitle()64 protected String getTitle() { 65 return getString(R.string.option_country_rating_systems); 66 } 67 68 @Override getTrackerLabel()69 public String getTrackerLabel() { 70 return TRACKER_LABEL; 71 } 72 73 @Override getItemList()74 protected List<Item> getItemList() { 75 ContentRatingsManager contentRatingsManager = getMainActivity().getContentRatingsManager(); 76 ParentalControlSettings parentalControlSettings = 77 getMainActivity().getParentalControlSettings(); 78 List<ContentRatingSystem> contentRatingSystems = 79 contentRatingsManager.getContentRatingSystems(); 80 Collections.sort(contentRatingSystems, ContentRatingSystem.DISPLAY_NAME_COMPARATOR); 81 List<Item> items = new ArrayList<>(); 82 List<Item> itemsHidden = new ArrayList<>(); 83 List<Item> itemsHiddenMultipleCountries = new ArrayList<>(); 84 85 // Add default, custom and preselected content rating systems to the "short" list. 86 for (ContentRatingSystem s : contentRatingSystems) { 87 if (!s.isCustom() 88 && s.getCountries() != null 89 && s.getCountries().contains(Locale.getDefault().getCountry())) { 90 items.add(new RatingSystemItem(s)); 91 } else if (s.isCustom() || parentalControlSettings.isContentRatingSystemEnabled(s)) { 92 items.add(new RatingSystemItem(s)); 93 } else { 94 List<String> countries = s.getCountries(); 95 if (countries.size() > 1) { 96 // Convert country codes to display names. 97 for (int i = 0; i < countries.size(); ++i) { 98 countries.set(i, new Locale("", countries.get(i)).getDisplayCountry()); 99 } 100 Collections.sort(countries); 101 StringBuilder builder = new StringBuilder(); 102 for (String country : countries) { 103 builder.append(country); 104 builder.append(", "); 105 } 106 itemsHiddenMultipleCountries.add( 107 new RatingSystemItem(s, builder.substring(0, builder.length() - 2))); 108 } else { 109 itemsHidden.add(new RatingSystemItem(s)); 110 } 111 } 112 } 113 114 // Add the rest of the content rating systems to the "long" list. 115 final List<Item> allItems = new ArrayList<>(items); 116 allItems.addAll(itemsHidden); 117 allItems.addAll(itemsHiddenMultipleCountries); 118 119 // Add "See All" to the "short" list. 120 items.add( 121 new ActionItem(getString(R.string.option_see_all_rating_systems)) { 122 @Override 123 protected void onSelected() { 124 setItems(allItems); 125 } 126 }); 127 return items; 128 } 129 setDefaultRatingSystemsIfNeeded(MainActivity tvActivity)130 private static void setDefaultRatingSystemsIfNeeded(MainActivity tvActivity) { 131 if (TvSettings.isContentRatingSystemSet(tvActivity)) { 132 return; 133 } 134 // Sets the default if the content rating system has never been set. 135 List<ContentRatingSystem> contentRatingSystems = 136 tvActivity.getContentRatingsManager().getContentRatingSystems(); 137 ContentRatingsManager manager = tvActivity.getContentRatingsManager(); 138 ParentalControlSettings settings = tvActivity.getParentalControlSettings(); 139 for (ContentRatingSystem s : contentRatingSystems) { 140 if (!s.isCustom() 141 && s.getCountries() != null 142 && s.getCountries().contains(Locale.getDefault().getCountry())) { 143 settings.setContentRatingSystemEnabled(manager, s, true); 144 } 145 } 146 } 147 148 private class RatingSystemItem extends CheckBoxItem { 149 private final ContentRatingSystem mContentRatingSystem; 150 RatingSystemItem(ContentRatingSystem contentRatingSystem)151 RatingSystemItem(ContentRatingSystem contentRatingSystem) { 152 this(contentRatingSystem, null); 153 } 154 RatingSystemItem(ContentRatingSystem contentRatingSystem, String description)155 RatingSystemItem(ContentRatingSystem contentRatingSystem, String description) { 156 super(contentRatingSystem.getDisplayName(), description, description != null); 157 mContentRatingSystem = contentRatingSystem; 158 } 159 160 @Override onUpdate()161 protected void onUpdate() { 162 super.onUpdate(); 163 setChecked( 164 getMainActivity() 165 .getParentalControlSettings() 166 .isContentRatingSystemEnabled(mContentRatingSystem)); 167 } 168 169 @Override onSelected()170 protected void onSelected() { 171 super.onSelected(); 172 getMainActivity() 173 .getParentalControlSettings() 174 .setContentRatingSystemEnabled( 175 getMainActivity().getContentRatingsManager(), 176 mContentRatingSystem, 177 isChecked()); 178 } 179 } 180 } 181