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.settings.device.sound;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.media.AudioManager;
22 import android.os.Bundle;
23 import android.provider.Settings;
24 import android.support.v14.preference.SwitchPreference;
25 import android.support.v17.preference.LeanbackPreferenceFragment;
26 import android.support.v7.preference.ListPreference;
27 import android.support.v7.preference.Preference;
28 import android.support.v7.preference.PreferenceScreen;
29 import android.support.v7.preference.TwoStatePreference;
30 import android.text.TextUtils;
31 
32 import com.android.tv.settings.R;
33 
34 public class SoundFragment extends LeanbackPreferenceFragment implements
35         Preference.OnPreferenceChangeListener {
36 
37     private static final String KEY_SOUND_EFFECTS = "sound_effects";
38     private static final String KEY_SURROUND_PASSTHROUGH = "surround_passthrough";
39 
40     private static final String VAL_SURROUND_SOUND_AUTO = "auto";
41     private static final String VAL_SURROUND_SOUND_ALWAYS = "always";
42     private static final String VAL_SURROUND_SOUND_NEVER = "never";
43 
44     private AudioManager mAudioManager;
45 
newInstance()46     public static SoundFragment newInstance() {
47         return new SoundFragment();
48     }
49 
50     @Override
onCreate(Bundle savedInstanceState)51     public void onCreate(Bundle savedInstanceState) {
52         mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
53         super.onCreate(savedInstanceState);
54     }
55 
56     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)57     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
58         setPreferencesFromResource(R.xml.sound, null);
59 
60         final TwoStatePreference soundPref = (TwoStatePreference) findPreference(KEY_SOUND_EFFECTS);
61         soundPref.setChecked(getSoundEffectsEnabled());
62 
63         final ListPreference surroundPref =
64                 (ListPreference) findPreference(KEY_SURROUND_PASSTHROUGH);
65         surroundPref.setValue(getSurroundPassthroughSetting());
66         surroundPref.setOnPreferenceChangeListener(this);
67     }
68 
69     @Override
onPreferenceTreeClick(Preference preference)70     public boolean onPreferenceTreeClick(Preference preference) {
71         if (TextUtils.equals(preference.getKey(), KEY_SOUND_EFFECTS)) {
72             final TwoStatePreference soundPref = (TwoStatePreference) preference;
73             setSoundEffectsEnabled(soundPref.isChecked());
74         }
75         return super.onPreferenceTreeClick(preference);
76     }
77 
78     @Override
onPreferenceChange(Preference preference, Object newValue)79     public boolean onPreferenceChange(Preference preference, Object newValue) {
80         if (TextUtils.equals(preference.getKey(), KEY_SURROUND_PASSTHROUGH)) {
81             final String selection = (String) newValue;
82             switch (selection) {
83                 case VAL_SURROUND_SOUND_AUTO:
84                     setSurroundPassthroughSetting(Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO);
85                     break;
86                 case VAL_SURROUND_SOUND_ALWAYS:
87                     setSurroundPassthroughSetting(Settings.Global.ENCODED_SURROUND_OUTPUT_ALWAYS);
88                     break;
89                 case VAL_SURROUND_SOUND_NEVER:
90                     setSurroundPassthroughSetting(Settings.Global.ENCODED_SURROUND_OUTPUT_NEVER);
91                     break;
92                 default:
93                     throw new IllegalArgumentException("Unknown surround sound pref value");
94             }
95             return true;
96         }
97         return true;
98     }
99 
getSoundEffectsEnabled()100     private boolean getSoundEffectsEnabled() {
101         return getSoundEffectsEnabled(getActivity().getContentResolver());
102     }
103 
getSoundEffectsEnabled(ContentResolver contentResolver)104     public static boolean getSoundEffectsEnabled(ContentResolver contentResolver) {
105         return Settings.System.getInt(contentResolver, Settings.System.SOUND_EFFECTS_ENABLED, 1)
106                 != 0;
107     }
108 
setSoundEffectsEnabled(boolean enabled)109     private void setSoundEffectsEnabled(boolean enabled) {
110         if (enabled) {
111             mAudioManager.loadSoundEffects();
112         } else {
113             mAudioManager.unloadSoundEffects();
114         }
115         Settings.System.putInt(getActivity().getContentResolver(),
116                 Settings.System.SOUND_EFFECTS_ENABLED, enabled ? 1 : 0);
117     }
118 
setSurroundPassthroughSetting(int newVal)119     private void setSurroundPassthroughSetting(int newVal) {
120         Settings.Global.putInt(getContext().getContentResolver(),
121                 Settings.Global.ENCODED_SURROUND_OUTPUT, newVal);
122     }
123 
getSurroundPassthroughSetting()124     private String getSurroundPassthroughSetting() {
125         final int value = Settings.Global.getInt(getContext().getContentResolver(),
126                 Settings.Global.ENCODED_SURROUND_OUTPUT,
127                 Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO);
128 
129         switch (value) {
130             case Settings.Global.ENCODED_SURROUND_OUTPUT_AUTO:
131             default:
132                 return VAL_SURROUND_SOUND_AUTO;
133             case Settings.Global.ENCODED_SURROUND_OUTPUT_ALWAYS:
134                 return VAL_SURROUND_SOUND_ALWAYS;
135             case Settings.Global.ENCODED_SURROUND_OUTPUT_NEVER:
136                 return VAL_SURROUND_SOUND_NEVER;
137         }
138     }
139 }
140