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