Home | History | Annotate | Download | only in parentalcontrols
      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 
     21 import com.android.tv.MainActivity;
     22 import com.android.tv.R;
     23 import com.android.tv.parental.ContentRatingSystem;
     24 import com.android.tv.parental.ContentRatingsManager;
     25 import com.android.tv.parental.ParentalControlSettings;
     26 import com.android.tv.ui.sidepanel.ActionItem;
     27 import com.android.tv.ui.sidepanel.CheckBoxItem;
     28 import com.android.tv.ui.sidepanel.Item;
     29 import com.android.tv.ui.sidepanel.SideFragment;
     30 import com.android.tv.util.TvSettings;
     31 
     32 import java.util.ArrayList;
     33 import java.util.Collections;
     34 import java.util.List;
     35 import java.util.Locale;
     36 
     37 public class RatingSystemsFragment extends SideFragment {
     38     private static final String TRACKER_LABEL = "Rating systems";
     39 
     40     @Override
     41     public void onCreate(Bundle savedInstanceState) {
     42         super.onCreate(savedInstanceState);
     43         setDefaultRatingSystemsIfNeeded((MainActivity) getActivity());
     44     }
     45 
     46     public static String getDescription(MainActivity tvActivity) {
     47         setDefaultRatingSystemsIfNeeded(tvActivity);
     48 
     49         List<ContentRatingSystem> contentRatingSystems =
     50                 tvActivity.getContentRatingsManager().getContentRatingSystems();
     51         Collections.sort(contentRatingSystems, ContentRatingSystem.DISPLAY_NAME_COMPARATOR);
     52         StringBuilder builder = new StringBuilder();
     53         for (ContentRatingSystem s : contentRatingSystems) {
     54             if (!tvActivity.getParentalControlSettings().isContentRatingSystemEnabled(s)) {
     55                 continue;
     56             }
     57             builder.append(s.getDisplayName());
     58             builder.append(", ");
     59         }
     60         return builder.length() > 0 ? builder.substring(0, builder.length() - 2)
     61                 : tvActivity.getString(R.string.option_no_enabled_rating_system);
     62     }
     63 
     64     @Override
     65     protected String getTitle() {
     66         return getString(R.string.option_country_rating_systems);
     67     }
     68 
     69     @Override
     70     public String getTrackerLabel() {
     71         return TRACKER_LABEL;
     72     }
     73 
     74     @Override
     75     protected List<Item> getItemList() {
     76         ContentRatingsManager contentRatingsManager = getMainActivity().getContentRatingsManager();
     77         ParentalControlSettings parentalControlSettings =
     78                 getMainActivity().getParentalControlSettings();
     79         List<ContentRatingSystem> contentRatingSystems =
     80                 contentRatingsManager.getContentRatingSystems();
     81         Collections.sort(contentRatingSystems, ContentRatingSystem.DISPLAY_NAME_COMPARATOR);
     82         List<Item> items = new ArrayList<>();
     83         List<Item> itemsHidden = new ArrayList<>();
     84         List<Item> itemsHiddenMultipleCountries = new ArrayList<>();
     85 
     86         // Add default, custom and preselected content rating systems to the "short" list.
     87         for (ContentRatingSystem s : contentRatingSystems) {
     88             if (!s.isCustom() && 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(new ActionItem(getString(R.string.option_see_all_rating_systems)) {
    121             @Override
    122             protected void onSelected() {
    123                 setItems(allItems);
    124             }
    125         });
    126         return items;
    127     }
    128 
    129     private static void setDefaultRatingSystemsIfNeeded(MainActivity tvActivity) {
    130         if (TvSettings.isContentRatingSystemSet(tvActivity)) {
    131             return;
    132         }
    133         // Sets the default if the content rating system has never been set.
    134         List<ContentRatingSystem> contentRatingSystems =
    135                 tvActivity.getContentRatingsManager().getContentRatingSystems();
    136         ContentRatingsManager manager = tvActivity.getContentRatingsManager();
    137         ParentalControlSettings settings = tvActivity.getParentalControlSettings();
    138         for (ContentRatingSystem s : contentRatingSystems) {
    139             if (!s.isCustom() && s.getCountries() != null
    140                     && s.getCountries().contains(Locale.getDefault().getCountry())) {
    141                 settings.setContentRatingSystemEnabled(manager, s, true);
    142             }
    143         }
    144     }
    145 
    146     private class RatingSystemItem extends CheckBoxItem {
    147         private final ContentRatingSystem mContentRatingSystem;
    148 
    149         RatingSystemItem(ContentRatingSystem contentRatingSystem) {
    150             this(contentRatingSystem, null);
    151         }
    152 
    153         RatingSystemItem(ContentRatingSystem contentRatingSystem, String description) {
    154             super(contentRatingSystem.getDisplayName(), description, description != null);
    155             mContentRatingSystem = contentRatingSystem;
    156         }
    157 
    158         @Override
    159         protected void onUpdate() {
    160             super.onUpdate();
    161             setChecked(getMainActivity().getParentalControlSettings()
    162                     .isContentRatingSystemEnabled(mContentRatingSystem));
    163         }
    164 
    165         @Override
    166         protected void onSelected() {
    167             super.onSelected();
    168             getMainActivity().getParentalControlSettings().setContentRatingSystemEnabled(
    169                     getMainActivity().getContentRatingsManager(), mContentRatingSystem,
    170                     isChecked());
    171         }
    172     }
    173 }
    174