• 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.content.Context;
20 import android.media.tv.TvContentRating;
21 import android.media.tv.TvContentRatingSystemInfo;
22 import android.support.annotation.Nullable;
23 import android.text.TextUtils;
24 import com.android.tv.R;
25 import com.android.tv.parental.ContentRatingSystem.Rating;
26 import com.android.tv.parental.ContentRatingSystem.SubRating;
27 import com.android.tv.util.TvInputManagerHelper;
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 public class ContentRatingsManager {
32     private final List<ContentRatingSystem> mContentRatingSystems = new ArrayList<>();
33 
34     private final Context mContext;
35     private final TvInputManagerHelper.TvInputManagerInterface mTvInputManager;
36 
ContentRatingsManager( Context context, TvInputManagerHelper.TvInputManagerInterface tvInputManager)37     public ContentRatingsManager(
38             Context context, TvInputManagerHelper.TvInputManagerInterface tvInputManager) {
39         mContext = context;
40         this.mTvInputManager = tvInputManager;
41     }
42 
update()43     public void update() {
44         mContentRatingSystems.clear();
45         ContentRatingsParser parser = new ContentRatingsParser(mContext);
46 
47         List<TvContentRatingSystemInfo> infos = mTvInputManager.getTvContentRatingSystemList();
48         for (TvContentRatingSystemInfo info : infos) {
49             List<ContentRatingSystem> list = parser.parse(info);
50             if (list != null) {
51                 mContentRatingSystems.addAll(list);
52             }
53         }
54     }
55 
56     /** Returns the content rating system with the give ID. */
57     @Nullable
getContentRatingSystem(String contentRatingSystemId)58     public ContentRatingSystem getContentRatingSystem(String contentRatingSystemId) {
59         for (ContentRatingSystem ratingSystem : mContentRatingSystems) {
60             if (TextUtils.equals(ratingSystem.getId(), contentRatingSystemId)) {
61                 return ratingSystem;
62             }
63         }
64         return null;
65     }
66 
67     /** Returns a new list of all content rating systems defined. */
getContentRatingSystems()68     public List<ContentRatingSystem> getContentRatingSystems() {
69         return new ArrayList<>(mContentRatingSystems);
70     }
71 
72     /**
73      * Returns the long name of a given content rating including descriptors (sub-ratings) that is
74      * displayed to the user. For example, "TV-PG (L, S)".
75      */
getDisplayNameForRating(TvContentRating canonicalRating)76     public String getDisplayNameForRating(TvContentRating canonicalRating) {
77         if (TvContentRating.UNRATED.equals(canonicalRating)) {
78             return mContext.getResources().getString(R.string.unrated_rating_name);
79         }
80         Rating rating = getRating(canonicalRating);
81         if (rating == null) {
82             return null;
83         }
84         List<SubRating> subRatings = getSubRatings(rating, canonicalRating);
85         if (!subRatings.isEmpty()) {
86             StringBuilder builder = new StringBuilder();
87             for (SubRating subRating : subRatings) {
88                 builder.append(subRating.getTitle());
89                 builder.append(", ");
90             }
91             return rating.getTitle() + " (" + builder.substring(0, builder.length() - 2) + ")";
92         }
93         return rating.getTitle();
94     }
95 
getRating(TvContentRating canonicalRating)96     private Rating getRating(TvContentRating canonicalRating) {
97         if (canonicalRating == null || mContentRatingSystems == null) {
98             return null;
99         }
100         for (ContentRatingSystem system : mContentRatingSystems) {
101             if (system.getDomain().equals(canonicalRating.getDomain())
102                     && system.getName().equals(canonicalRating.getRatingSystem())) {
103                 for (Rating rating : system.getRatings()) {
104                     if (rating.getName().equals(canonicalRating.getMainRating())) {
105                         return rating;
106                     }
107                 }
108             }
109         }
110         return null;
111     }
112 
getSubRatings(Rating rating, TvContentRating canonicalRating)113     private List<SubRating> getSubRatings(Rating rating, TvContentRating canonicalRating) {
114         List<SubRating> subRatings = new ArrayList<>();
115         if (rating == null
116                 || rating.getSubRatings() == null
117                 || canonicalRating == null
118                 || canonicalRating.getSubRatings() == null) {
119             return subRatings;
120         }
121         for (String subRatingString : canonicalRating.getSubRatings()) {
122             for (SubRating subRating : rating.getSubRatings()) {
123                 if (subRating.getName().equals(subRatingString)) {
124                     subRatings.add(subRating);
125                     break;
126                 }
127             }
128         }
129         return subRatings;
130     }
131 }
132