Home | History | Annotate | Download | only in parental
      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.parental;
     18 
     19 import android.media.tv.TvContentRating;
     20 
     21 import com.android.tv.parental.ContentRatingSystem.Rating;
     22 import com.android.tv.parental.ContentRatingSystem.SubRating;
     23 import com.android.tv.util.TvSettings;
     24 import com.android.tv.util.TvSettings.ContentRatingLevel;
     25 
     26 import java.util.HashSet;
     27 import java.util.Set;
     28 
     29 public class ContentRatingLevelPolicy {
     30     private static final int AGE_THRESHOLD_FOR_LEVEL_HIGH = 6;
     31     private static final int AGE_THRESHOLD_FOR_LEVEL_MEDIUM = 12;
     32     private static final int AGE_THRESHOLD_FOR_LEVEL_LOW = -1; // Highest age for each rating system
     33 
     34     private ContentRatingLevelPolicy() { }
     35 
     36     public static Set<TvContentRating> getRatingsForLevel(
     37             ParentalControlSettings settings, ContentRatingsManager manager,
     38             @ContentRatingLevel int level) {
     39         if (level == TvSettings.CONTENT_RATING_LEVEL_NONE) {
     40             return new HashSet<>();
     41         } else if (level == TvSettings.CONTENT_RATING_LEVEL_HIGH) {
     42             return getRatingsForAge(settings, manager, AGE_THRESHOLD_FOR_LEVEL_HIGH);
     43         } else if (level == TvSettings.CONTENT_RATING_LEVEL_MEDIUM) {
     44             return getRatingsForAge(settings, manager, AGE_THRESHOLD_FOR_LEVEL_MEDIUM);
     45         } else if (level == TvSettings.CONTENT_RATING_LEVEL_LOW) {
     46             return getRatingsForAge(settings, manager, AGE_THRESHOLD_FOR_LEVEL_LOW);
     47         }
     48         throw new IllegalArgumentException("Unexpected rating level");
     49     }
     50 
     51     private static Set<TvContentRating> getRatingsForAge(
     52             ParentalControlSettings settings, ContentRatingsManager manager, int age) {
     53         Set<TvContentRating> ratings = new HashSet<>();
     54 
     55         for (ContentRatingSystem contentRatingSystem : manager.getContentRatingSystems()) {
     56             if (!settings.isContentRatingSystemEnabled(contentRatingSystem)) {
     57                 continue;
     58             }
     59             int ageLimit = age;
     60             if (ageLimit == AGE_THRESHOLD_FOR_LEVEL_LOW) {
     61                 ageLimit = getMaxAge(contentRatingSystem);
     62             }
     63             for (Rating rating : contentRatingSystem.getRatings()) {
     64                 if (rating.getAgeHint() < ageLimit) {
     65                     continue;
     66                 }
     67                 TvContentRating tvContentRating = TvContentRating.createRating(
     68                         contentRatingSystem.getDomain(), contentRatingSystem.getName(),
     69                         rating.getName());
     70                 ratings.add(tvContentRating);
     71                 for (SubRating subRating : rating.getSubRatings()) {
     72                     tvContentRating = TvContentRating.createRating(
     73                             contentRatingSystem.getDomain(), contentRatingSystem.getName(),
     74                             rating.getName(), subRating.getName());
     75                     ratings.add(tvContentRating);
     76                 }
     77             }
     78         }
     79 
     80         return ratings;
     81     }
     82 
     83     private static int getMaxAge(ContentRatingSystem contentRatingSystem) {
     84         int maxAge = 0;
     85         for (Rating rating : contentRatingSystem.getRatings()) {
     86             if (maxAge < rating.getAgeHint()) {
     87                 maxAge = rating.getAgeHint();
     88             }
     89         }
     90         return maxAge;
     91     }
     92 }
     93