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