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