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