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.util;
18 
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.media.tv.TvTrackInfo;
22 import android.preference.PreferenceManager;
23 import android.support.annotation.IntDef;
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 import java.util.Collections;
27 import java.util.HashSet;
28 import java.util.Set;
29 
30 /**
31  * A class about the constants for TV settings. Objects that are returned from the various {@code
32  * get} methods must be treated as immutable.
33  */
34 public final class TvSettings {
35     public static final String PREF_DISPLAY_MODE = "display_mode"; // int value
36     public static final String PREF_PIN = "pin"; // 4-digit string value. Otherwise, it's not set.
37 
38     // Multi-track audio settings
39     private static final String PREF_MULTI_AUDIO_ID = "pref.multi_audio_id";
40     private static final String PREF_MULTI_AUDIO_LANGUAGE = "pref.multi_audio_language";
41     private static final String PREF_MULTI_AUDIO_CHANNEL_COUNT = "pref.multi_audio_channel_count";
42 
43     // DVR Multi-audio and subtitle settings
44     private static final String PREF_DVR_MULTI_AUDIO_ID = "pref.dvr_multi_audio_id";
45     private static final String PREF_DVR_MULTI_AUDIO_LANGUAGE = "pref.dvr_multi_audio_language";
46     private static final String PREF_DVR_MULTI_AUDIO_CHANNEL_COUNT =
47             "pref.dvr_multi_audio_channel_count";
48     private static final String PREF_DVR_SUBTITLE_ID = "pref.dvr_subtitle_id";
49     private static final String PREF_DVR_SUBTITLE_LANGUAGE = "pref.dvr_subtitle_language";
50 
51     // Parental Control settings
52     private static final String PREF_CONTENT_RATING_SYSTEMS = "pref.content_rating_systems";
53     private static final String PREF_CONTENT_RATING_LEVEL = "pref.content_rating_level";
54     private static final String PREF_DISABLE_PIN_UNTIL = "pref.disable_pin_until";
55 
56     @Retention(RetentionPolicy.SOURCE)
57     @IntDef({
58         CONTENT_RATING_LEVEL_NONE,
59         CONTENT_RATING_LEVEL_HIGH,
60         CONTENT_RATING_LEVEL_MEDIUM,
61         CONTENT_RATING_LEVEL_LOW,
62         CONTENT_RATING_LEVEL_CUSTOM
63     })
64     public @interface ContentRatingLevel {}
65 
66     public static final int CONTENT_RATING_LEVEL_NONE = 0;
67     public static final int CONTENT_RATING_LEVEL_HIGH = 1;
68     public static final int CONTENT_RATING_LEVEL_MEDIUM = 2;
69     public static final int CONTENT_RATING_LEVEL_LOW = 3;
70     public static final int CONTENT_RATING_LEVEL_CUSTOM = 4;
71 
TvSettings()72     private TvSettings() {}
73 
74     // Multi-track audio settings
getMultiAudioId(Context context)75     public static String getMultiAudioId(Context context) {
76         return PreferenceManager.getDefaultSharedPreferences(context)
77                 .getString(PREF_MULTI_AUDIO_ID, null);
78     }
79 
setMultiAudioId(Context context, String language)80     public static void setMultiAudioId(Context context, String language) {
81         PreferenceManager.getDefaultSharedPreferences(context)
82                 .edit()
83                 .putString(PREF_MULTI_AUDIO_ID, language)
84                 .apply();
85     }
86 
getMultiAudioLanguage(Context context)87     public static String getMultiAudioLanguage(Context context) {
88         return PreferenceManager.getDefaultSharedPreferences(context)
89                 .getString(PREF_MULTI_AUDIO_LANGUAGE, null);
90     }
91 
setMultiAudioLanguage(Context context, String language)92     public static void setMultiAudioLanguage(Context context, String language) {
93         PreferenceManager.getDefaultSharedPreferences(context)
94                 .edit()
95                 .putString(PREF_MULTI_AUDIO_LANGUAGE, language)
96                 .apply();
97     }
98 
getMultiAudioChannelCount(Context context)99     public static int getMultiAudioChannelCount(Context context) {
100         return PreferenceManager.getDefaultSharedPreferences(context)
101                 .getInt(PREF_MULTI_AUDIO_CHANNEL_COUNT, 0);
102     }
103 
setMultiAudioChannelCount(Context context, int channelCount)104     public static void setMultiAudioChannelCount(Context context, int channelCount) {
105         PreferenceManager.getDefaultSharedPreferences(context)
106                 .edit()
107                 .putInt(PREF_MULTI_AUDIO_CHANNEL_COUNT, channelCount)
108                 .apply();
109     }
110 
setDvrPlaybackTrackSettings( Context context, int trackType, TvTrackInfo info)111     public static void setDvrPlaybackTrackSettings(
112             Context context, int trackType, TvTrackInfo info) {
113         if (trackType == TvTrackInfo.TYPE_AUDIO) {
114             if (info == null) {
115                 PreferenceManager.getDefaultSharedPreferences(context)
116                         .edit()
117                         .remove(PREF_DVR_MULTI_AUDIO_ID)
118                         .apply();
119             } else {
120                 PreferenceManager.getDefaultSharedPreferences(context)
121                         .edit()
122                         .putString(PREF_DVR_MULTI_AUDIO_LANGUAGE, info.getLanguage())
123                         .putInt(PREF_DVR_MULTI_AUDIO_CHANNEL_COUNT, info.getAudioChannelCount())
124                         .putString(PREF_DVR_MULTI_AUDIO_ID, info.getId())
125                         .apply();
126             }
127         } else if (trackType == TvTrackInfo.TYPE_SUBTITLE) {
128             if (info == null) {
129                 PreferenceManager.getDefaultSharedPreferences(context)
130                         .edit()
131                         .remove(PREF_DVR_SUBTITLE_ID)
132                         .apply();
133             } else {
134                 PreferenceManager.getDefaultSharedPreferences(context)
135                         .edit()
136                         .putString(PREF_DVR_SUBTITLE_LANGUAGE, info.getLanguage())
137                         .putString(PREF_DVR_SUBTITLE_ID, info.getId())
138                         .apply();
139             }
140         }
141     }
142 
getDvrPlaybackTrackSettings(Context context, int trackType)143     public static TvTrackInfo getDvrPlaybackTrackSettings(Context context, int trackType) {
144         String language;
145         String trackId;
146         int channelCount;
147         SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
148         if (trackType == TvTrackInfo.TYPE_AUDIO) {
149             trackId = pref.getString(PREF_DVR_MULTI_AUDIO_ID, null);
150             if (trackId == null) {
151                 return null;
152             }
153             language = pref.getString(PREF_DVR_MULTI_AUDIO_LANGUAGE, null);
154             channelCount = pref.getInt(PREF_DVR_MULTI_AUDIO_CHANNEL_COUNT, 0);
155             return new TvTrackInfo.Builder(trackType, trackId)
156                     .setLanguage(language)
157                     .setAudioChannelCount(channelCount)
158                     .build();
159         } else if (trackType == TvTrackInfo.TYPE_SUBTITLE) {
160             trackId = pref.getString(PREF_DVR_SUBTITLE_ID, null);
161             if (trackId == null) {
162                 return null;
163             }
164             language = pref.getString(PREF_DVR_SUBTITLE_LANGUAGE, null);
165             return new TvTrackInfo.Builder(trackType, trackId).setLanguage(language).build();
166         } else {
167             return null;
168         }
169     }
170 
171     // Parental Control settings
addContentRatingSystem(Context context, String id)172     public static void addContentRatingSystem(Context context, String id) {
173         Set<String> contentRatingSystemSet = getContentRatingSystemSet(context);
174         if (contentRatingSystemSet.add(id)) {
175             PreferenceManager.getDefaultSharedPreferences(context)
176                     .edit()
177                     .putStringSet(PREF_CONTENT_RATING_SYSTEMS, contentRatingSystemSet)
178                     .apply();
179         }
180     }
181 
removeContentRatingSystem(Context context, String id)182     public static void removeContentRatingSystem(Context context, String id) {
183         Set<String> contentRatingSystemSet = getContentRatingSystemSet(context);
184         if (contentRatingSystemSet.remove(id)) {
185             PreferenceManager.getDefaultSharedPreferences(context)
186                     .edit()
187                     .putStringSet(PREF_CONTENT_RATING_SYSTEMS, contentRatingSystemSet)
188                     .apply();
189         }
190     }
191 
hasContentRatingSystem(Context context, String id)192     public static boolean hasContentRatingSystem(Context context, String id) {
193         return getContentRatingSystemSet(context).contains(id);
194     }
195 
196     /**
197      * Returns whether the content rating system is ever set. Returns {@code false} only when the
198      * user changes parental control settings for the first time.
199      */
isContentRatingSystemSet(Context context)200     public static boolean isContentRatingSystemSet(Context context) {
201         return PreferenceManager.getDefaultSharedPreferences(context)
202                         .getStringSet(PREF_CONTENT_RATING_SYSTEMS, null)
203                 != null;
204     }
205 
getContentRatingSystemSet(Context context)206     private static Set<String> getContentRatingSystemSet(Context context) {
207         return new HashSet<>(
208                 PreferenceManager.getDefaultSharedPreferences(context)
209                         .getStringSet(PREF_CONTENT_RATING_SYSTEMS, Collections.emptySet()));
210     }
211 
212     @ContentRatingLevel
213     @SuppressWarnings("ResourceType")
getContentRatingLevel(Context context)214     public static int getContentRatingLevel(Context context) {
215         return PreferenceManager.getDefaultSharedPreferences(context)
216                 .getInt(PREF_CONTENT_RATING_LEVEL, CONTENT_RATING_LEVEL_NONE);
217     }
218 
setContentRatingLevel(Context context, @ContentRatingLevel int level)219     public static void setContentRatingLevel(Context context, @ContentRatingLevel int level) {
220         PreferenceManager.getDefaultSharedPreferences(context)
221                 .edit()
222                 .putInt(PREF_CONTENT_RATING_LEVEL, level)
223                 .apply();
224     }
225 
226     /**
227      * Returns the time until we should disable the PIN dialog (because the user input wrong PINs
228      * repeatedly).
229      */
getDisablePinUntil(Context context)230     public static long getDisablePinUntil(Context context) {
231         return PreferenceManager.getDefaultSharedPreferences(context)
232                 .getLong(PREF_DISABLE_PIN_UNTIL, 0);
233     }
234 
235     /**
236      * Saves the time until we should disable the PIN dialog (because the user input wrong PINs
237      * repeatedly).
238      */
setDisablePinUntil(Context context, long timeMillis)239     public static void setDisablePinUntil(Context context, long timeMillis) {
240         PreferenceManager.getDefaultSharedPreferences(context)
241                 .edit()
242                 .putLong(PREF_DISABLE_PIN_UNTIL, timeMillis)
243                 .apply();
244     }
245 }
246