aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/parental/ParentalControlSettings.java
blob: d7e1846ea8412ad6d512de0c4ce883387fe09b9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.parental;

import android.content.Context;
import android.media.tv.TvContentRating;
import android.media.tv.TvInputManager;

import com.android.tv.parental.ContentRatingSystem.Rating;
import com.android.tv.parental.ContentRatingSystem.SubRating;
import com.android.tv.util.TvSettings;
import com.android.tv.util.TvSettings.ContentRatingLevel;

import java.util.HashSet;
import java.util.Set;

public class ParentalControlSettings {
    /**
     * The rating and all of its sub-ratings are blocked.
     */
    public static final int RATING_BLOCKED = 0;

    /**
     * The rating is blocked but not all of its sub-ratings are blocked.
     */
    public static final int RATING_BLOCKED_PARTIAL = 1;

    /**
     * The rating is not blocked.
     */
    public static final int RATING_NOT_BLOCKED = 2;

    private final Context mContext;
    private final TvInputManager mTvInputManager;

    // mRatings is expected to be synchronized with mTvInputManager.getBlockedRatings().
    private Set<TvContentRating> mRatings;
    private Set<TvContentRating> mCustomRatings;

    public ParentalControlSettings(Context context) {
        mContext = context;
        mTvInputManager = (TvInputManager) mContext.getSystemService(Context.TV_INPUT_SERVICE);
    }

    public boolean isParentalControlsEnabled() {
        return mTvInputManager.isParentalControlsEnabled();
    }

    public void setParentalControlsEnabled(boolean enabled) {
        mTvInputManager.setParentalControlsEnabled(enabled);
    }

    public void setContentRatingSystemEnabled(ContentRatingsManager manager,
            ContentRatingSystem contentRatingSystem, boolean enabled) {
        if (enabled) {
            TvSettings.addContentRatingSystem(mContext, contentRatingSystem.getId());

            // Ensure newly added system has ratings for current level set
            updateRatingsForCurrentLevel(manager);
        } else {
            // Ensure no ratings are blocked for the selected rating system
            for (TvContentRating tvContentRating : mTvInputManager.getBlockedRatings()) {
                if (contentRatingSystem.ownsRating(tvContentRating)) {
                    mTvInputManager.removeBlockedRating(tvContentRating);
                }
            }

            TvSettings.removeContentRatingSystem(mContext, contentRatingSystem.getId());
        }
    }

    public boolean isContentRatingSystemEnabled(ContentRatingSystem contentRatingSystem) {
        return TvSettings.hasContentRatingSystem(mContext, contentRatingSystem.getId());
    }

    public void loadRatings() {
        mRatings = new HashSet<>(mTvInputManager.getBlockedRatings());
    }

    private void storeRatings() {
        Set<TvContentRating> removed = new HashSet<>(mTvInputManager.getBlockedRatings());
        removed.removeAll(mRatings);
        for (TvContentRating tvContentRating : removed) {
            mTvInputManager.removeBlockedRating(tvContentRating);
        }

        Set<TvContentRating> added = new HashSet<>(mRatings);
        added.removeAll(mTvInputManager.getBlockedRatings());
        for (TvContentRating tvContentRating : added) {
            mTvInputManager.addBlockedRating(tvContentRating);
        }
    }

    private void updateRatingsForCurrentLevel(ContentRatingsManager manager) {
        @ContentRatingLevel int currentLevel = getContentRatingLevel();
        if (currentLevel != TvSettings.CONTENT_RATING_LEVEL_CUSTOM) {
            mRatings = ContentRatingLevelPolicy.getRatingsForLevel(this, manager, currentLevel);
            storeRatings();
        }
    }

    public void setContentRatingLevel(ContentRatingsManager manager,
            @ContentRatingLevel int level) {
        @ContentRatingLevel int currentLevel = getContentRatingLevel();
        if (level == currentLevel) {
            return;
        }
        if (currentLevel == TvSettings.CONTENT_RATING_LEVEL_CUSTOM) {
            mCustomRatings = mRatings;
        }
        TvSettings.setContentRatingLevel(mContext, level);
        if (level == TvSettings.CONTENT_RATING_LEVEL_CUSTOM) {
            if (mCustomRatings != null) {
                mRatings = new HashSet<>(mCustomRatings);
            }
        } else {
            mRatings = ContentRatingLevelPolicy.getRatingsForLevel(this, manager, level);
        }
        storeRatings();
    }

    @ContentRatingLevel
    public int getContentRatingLevel() {
        return TvSettings.getContentRatingLevel(mContext);
    }

    /**
     * Sets the blocked status of a given content rating.
     * <p>
     * Note that a call to this method automatically changes the current rating level to
     * {@code TvSettings.CONTENT_RATING_LEVEL_CUSTOM} if needed.
     * </p>
     *
     * @param contentRatingSystem The content rating system where the given rating belongs.
     * @param rating The content rating to set.
     * @return {@code true} if changed, {@code false} otherwise.
     * @see #setSubRatingBlocked
     */
    public boolean setRatingBlocked(ContentRatingSystem contentRatingSystem, Rating rating,
            boolean blocked) {
        return setRatingBlockedInternal(contentRatingSystem, rating, null, blocked);
    }

    /**
     * Checks whether any of given ratings is blocked.
     *
     * @param ratings The array of ratings to check
     * @return {@code true} if a rating is blocked, {@code false} otherwise.
     */
    public boolean isRatingBlocked(TvContentRating[] ratings) {
        return getBlockedRating(ratings) != null;
    }

    /**
     * Checks whether any of given ratings is blocked and returns the first blocked rating.
     *
     * @param ratings The array of ratings to check
     * @return The {@link TvContentRating} that is blocked.
     */
    public TvContentRating getBlockedRating(TvContentRating[] ratings) {
        if (ratings == null) {
            return null;
        }
        for (TvContentRating rating : ratings) {
            if (mTvInputManager.isRatingBlocked(rating)) {
                return rating;
            }
        }
        return null;
    }

    /**
     * Checks whether a given rating is blocked by the user or not.
     *
     * @param contentRatingSystem The content rating system where the given rating belongs.
     * @param rating The content rating to check.
     * @return {@code true} if blocked, {@code false} otherwise.
     */
    public boolean isRatingBlocked(ContentRatingSystem contentRatingSystem, Rating rating) {
        return mRatings.contains(toTvContentRating(contentRatingSystem, rating));
    }

    /**
     * Sets the blocked status of a given content sub-rating.
     * <p>
     * Note that a call to this method automatically changes the current rating level to
     * {@code TvSettings.CONTENT_RATING_LEVEL_CUSTOM} if needed.
     * </p>
     *
     * @param contentRatingSystem The content rating system where the given rating belongs.
     * @param rating The content rating associated with the given sub-rating.
     * @param subRating The content sub-rating to set.
     * @return {@code true} if changed, {@code false} otherwise.
     * @see #setRatingBlocked
     */
    public boolean setSubRatingBlocked(ContentRatingSystem contentRatingSystem, Rating rating,
            SubRating subRating, boolean blocked) {
        return setRatingBlockedInternal(contentRatingSystem, rating, subRating, blocked);
    }

    /**
     * Checks whether a given content sub-rating is blocked by the user or not.
     *
     * @param contentRatingSystem The content rating system where the given rating belongs.
     * @param rating The content rating associated with the given sub-rating.
     * @param subRating The content sub-rating to check.
     * @return {@code true} if blocked, {@code false} otherwise.
     */
    public boolean isSubRatingEnabled(ContentRatingSystem contentRatingSystem, Rating rating,
            SubRating subRating) {
        return mRatings.contains(toTvContentRating(contentRatingSystem, rating, subRating));
    }

    private boolean setRatingBlockedInternal(ContentRatingSystem contentRatingSystem, Rating rating,
            SubRating subRating, boolean blocked) {
        TvContentRating tvContentRating = (subRating == null)
                ? toTvContentRating(contentRatingSystem, rating)
                : toTvContentRating(contentRatingSystem, rating, subRating);
        boolean changed;
        if (blocked) {
            changed = mRatings.add(tvContentRating);
            mTvInputManager.addBlockedRating(tvContentRating);
        } else {
            changed = mRatings.remove(tvContentRating);
            mTvInputManager.removeBlockedRating(tvContentRating);
        }
        if (changed) {
            changeToCustomLevel();
        }
        return changed;
    }

    private void changeToCustomLevel() {
        if (getContentRatingLevel() != TvSettings.CONTENT_RATING_LEVEL_CUSTOM) {
            TvSettings.setContentRatingLevel(mContext, TvSettings.CONTENT_RATING_LEVEL_CUSTOM);
        }
    }

    /**
     * Returns the blocked status of a given rating. The status can be one of the followings:
     * {@link #RATING_BLOCKED}, {@link #RATING_BLOCKED_PARTIAL} and {@link #RATING_NOT_BLOCKED}
     */
    public int getBlockedStatus(ContentRatingSystem contentRatingSystem, Rating rating) {
        if (isRatingBlocked(contentRatingSystem, rating)) {
            return RATING_BLOCKED;
        }
        for (SubRating subRating : rating.getSubRatings()) {
            if (isSubRatingEnabled(contentRatingSystem, rating, subRating)) {
                return RATING_BLOCKED_PARTIAL;
            }
        }
        return RATING_NOT_BLOCKED;
    }

    private TvContentRating toTvContentRating(ContentRatingSystem contentRatingSystem,
            Rating rating) {
        return TvContentRating.createRating(contentRatingSystem.getDomain(),
                contentRatingSystem.getName(), rating.getName());
    }

    private TvContentRating toTvContentRating(ContentRatingSystem contentRatingSystem,
            Rating rating, SubRating subRating) {
        return TvContentRating.createRating(contentRatingSystem.getDomain(),
                contentRatingSystem.getName(), rating.getName(), subRating.getName());
    }
}