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