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     // tviapp settings
57     private static final String PREF_TV_IAPP_STATES = "pref.tviapp_on";
58 
59     @Retention(RetentionPolicy.SOURCE)
60     @IntDef({
61         CONTENT_RATING_LEVEL_NONE,
62         CONTENT_RATING_LEVEL_HIGH,
63         CONTENT_RATING_LEVEL_MEDIUM,
64         CONTENT_RATING_LEVEL_LOW,
65         CONTENT_RATING_LEVEL_CUSTOM
66     })
67     public @interface ContentRatingLevel {}
68 
69     public static final int CONTENT_RATING_LEVEL_NONE = 0;
70     public static final int CONTENT_RATING_LEVEL_HIGH = 1;
71     public static final int CONTENT_RATING_LEVEL_MEDIUM = 2;
72     public static final int CONTENT_RATING_LEVEL_LOW = 3;
73     public static final int CONTENT_RATING_LEVEL_CUSTOM = 4;
74 
TvSettings()75     private TvSettings() {}
76 
77     // Multi-track audio settings
getMultiAudioId(Context context)78     public static String getMultiAudioId(Context context) {
79         return PreferenceManager.getDefaultSharedPreferences(context)
80                 .getString(PREF_MULTI_AUDIO_ID, null);
81     }
82 
setMultiAudioId(Context context, String language)83     public static void setMultiAudioId(Context context, String language) {
84         PreferenceManager.getDefaultSharedPreferences(context)
85                 .edit()
86                 .putString(PREF_MULTI_AUDIO_ID, language)
87                 .apply();
88     }
89 
getMultiAudioLanguage(Context context)90     public static String getMultiAudioLanguage(Context context) {
91         return PreferenceManager.getDefaultSharedPreferences(context)
92                 .getString(PREF_MULTI_AUDIO_LANGUAGE, null);
93     }
94 
setMultiAudioLanguage(Context context, String language)95     public static void setMultiAudioLanguage(Context context, String language) {
96         PreferenceManager.getDefaultSharedPreferences(context)
97                 .edit()
98                 .putString(PREF_MULTI_AUDIO_LANGUAGE, language)
99                 .apply();
100     }
101 
getMultiAudioChannelCount(Context context)102     public static int getMultiAudioChannelCount(Context context) {
103         return PreferenceManager.getDefaultSharedPreferences(context)
104                 .getInt(PREF_MULTI_AUDIO_CHANNEL_COUNT, 0);
105     }
106 
setMultiAudioChannelCount(Context context, int channelCount)107     public static void setMultiAudioChannelCount(Context context, int channelCount) {
108         PreferenceManager.getDefaultSharedPreferences(context)
109                 .edit()
110                 .putInt(PREF_MULTI_AUDIO_CHANNEL_COUNT, channelCount)
111                 .apply();
112     }
113 
setDvrPlaybackTrackSettings( Context context, int trackType, TvTrackInfo info)114     public static void setDvrPlaybackTrackSettings(
115             Context context, int trackType, TvTrackInfo info) {
116         if (trackType == TvTrackInfo.TYPE_AUDIO) {
117             if (info == null) {
118                 PreferenceManager.getDefaultSharedPreferences(context)
119                         .edit()
120                         .remove(PREF_DVR_MULTI_AUDIO_ID)
121                         .apply();
122             } else {
123                 PreferenceManager.getDefaultSharedPreferences(context)
124                         .edit()
125                         .putString(PREF_DVR_MULTI_AUDIO_LANGUAGE, info.getLanguage())
126                         .putInt(PREF_DVR_MULTI_AUDIO_CHANNEL_COUNT, info.getAudioChannelCount())
127                         .putString(PREF_DVR_MULTI_AUDIO_ID, info.getId())
128                         .apply();
129             }
130         } else if (trackType == TvTrackInfo.TYPE_SUBTITLE) {
131             if (info == null) {
132                 PreferenceManager.getDefaultSharedPreferences(context)
133                         .edit()
134                         .remove(PREF_DVR_SUBTITLE_ID)
135                         .apply();
136             } else {
137                 PreferenceManager.getDefaultSharedPreferences(context)
138                         .edit()
139                         .putString(PREF_DVR_SUBTITLE_LANGUAGE, info.getLanguage())
140                         .putString(PREF_DVR_SUBTITLE_ID, info.getId())
141                         .apply();
142             }
143         }
144     }
145 
getDvrPlaybackTrackSettings(Context context, int trackType)146     public static TvTrackInfo getDvrPlaybackTrackSettings(Context context, int trackType) {
147         String language;
148         String trackId;
149         int channelCount;
150         SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
151         if (trackType == TvTrackInfo.TYPE_AUDIO) {
152             trackId = pref.getString(PREF_DVR_MULTI_AUDIO_ID, null);
153             if (trackId == null) {
154                 return null;
155             }
156             language = pref.getString(PREF_DVR_MULTI_AUDIO_LANGUAGE, null);
157             channelCount = pref.getInt(PREF_DVR_MULTI_AUDIO_CHANNEL_COUNT, 0);
158             return new TvTrackInfo.Builder(trackType, trackId)
159                     .setLanguage(language)
160                     .setAudioChannelCount(channelCount)
161                     .build();
162         } else if (trackType == TvTrackInfo.TYPE_SUBTITLE) {
163             trackId = pref.getString(PREF_DVR_SUBTITLE_ID, null);
164             if (trackId == null) {
165                 return null;
166             }
167             language = pref.getString(PREF_DVR_SUBTITLE_LANGUAGE, null);
168             return new TvTrackInfo.Builder(trackType, trackId).setLanguage(language).build();
169         } else {
170             return null;
171         }
172     }
173 
174     // Parental Control settings
addContentRatingSystem(Context context, String id)175     public static void addContentRatingSystem(Context context, String id) {
176         Set<String> contentRatingSystemSet = getContentRatingSystemSet(context);
177         if (contentRatingSystemSet.add(id)) {
178             PreferenceManager.getDefaultSharedPreferences(context)
179                     .edit()
180                     .putStringSet(PREF_CONTENT_RATING_SYSTEMS, contentRatingSystemSet)
181                     .apply();
182         }
183     }
184 
removeContentRatingSystem(Context context, String id)185     public static void removeContentRatingSystem(Context context, String id) {
186         Set<String> contentRatingSystemSet = getContentRatingSystemSet(context);
187         if (contentRatingSystemSet.remove(id)) {
188             PreferenceManager.getDefaultSharedPreferences(context)
189                     .edit()
190                     .putStringSet(PREF_CONTENT_RATING_SYSTEMS, contentRatingSystemSet)
191                     .apply();
192         }
193     }
194 
hasContentRatingSystem(Context context, String id)195     public static boolean hasContentRatingSystem(Context context, String id) {
196         return getContentRatingSystemSet(context).contains(id);
197     }
198 
199     /**
200      * Returns whether the content rating system is ever set. Returns {@code false} only when the
201      * user changes parental control settings for the first time.
202      */
isContentRatingSystemSet(Context context)203     public static boolean isContentRatingSystemSet(Context context) {
204         return PreferenceManager.getDefaultSharedPreferences(context)
205                         .getStringSet(PREF_CONTENT_RATING_SYSTEMS, null)
206                 != null;
207     }
208 
getContentRatingSystemSet(Context context)209     private static Set<String> getContentRatingSystemSet(Context context) {
210         return new HashSet<>(
211                 PreferenceManager.getDefaultSharedPreferences(context)
212                         .getStringSet(PREF_CONTENT_RATING_SYSTEMS, Collections.emptySet()));
213     }
214 
215     @ContentRatingLevel
216     @SuppressWarnings("ResourceType")
getContentRatingLevel(Context context)217     public static int getContentRatingLevel(Context context) {
218         return PreferenceManager.getDefaultSharedPreferences(context)
219                 .getInt(PREF_CONTENT_RATING_LEVEL, CONTENT_RATING_LEVEL_NONE);
220     }
221 
setContentRatingLevel(Context context, @ContentRatingLevel int level)222     public static void setContentRatingLevel(Context context, @ContentRatingLevel int level) {
223         PreferenceManager.getDefaultSharedPreferences(context)
224                 .edit()
225                 .putInt(PREF_CONTENT_RATING_LEVEL, level)
226                 .apply();
227     }
228 
229     /**
230      * Returns the time until we should disable the PIN dialog (because the user input wrong PINs
231      * repeatedly).
232      */
getDisablePinUntil(Context context)233     public static long getDisablePinUntil(Context context) {
234         return PreferenceManager.getDefaultSharedPreferences(context)
235                 .getLong(PREF_DISABLE_PIN_UNTIL, 0);
236     }
237 
238     /**
239      * Saves the time until we should disable the PIN dialog (because the user input wrong PINs
240      * repeatedly).
241      */
setDisablePinUntil(Context context, long timeMillis)242     public static void setDisablePinUntil(Context context, long timeMillis) {
243         PreferenceManager.getDefaultSharedPreferences(context)
244                 .edit()
245                 .putLong(PREF_DISABLE_PIN_UNTIL, timeMillis)
246                 .apply();
247     }
248 
isTvIAppOn(Context context)249     public static boolean isTvIAppOn(Context context) {
250         return PreferenceManager.getDefaultSharedPreferences(context)
251                 .getBoolean(PREF_TV_IAPP_STATES, false);
252     }
253 
setTvIAppOn(Context context, boolean isOn)254     public static void setTvIAppOn(Context context, boolean isOn) {
255         PreferenceManager.getDefaultSharedPreferences(context)
256                 .edit()
257                 .putBoolean(PREF_TV_IAPP_STATES, isOn)
258                 .apply();
259     }
260 }
261