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.displaysound; 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.text.TextUtils; 25 26 import androidx.annotation.Keep; 27 import androidx.preference.Preference; 28 import androidx.preference.TwoStatePreference; 29 30 import com.android.tv.settings.R; 31 import com.android.tv.settings.SettingsPreferenceFragment; 32 33 /** 34 * The "Display & sound" screen in TV Settings. 35 */ 36 @Keep 37 public class DisplaySoundFragment extends SettingsPreferenceFragment { 38 39 static final String KEY_SOUND_EFFECTS = "sound_effects"; 40 41 private AudioManager mAudioManager; 42 newInstance()43 public static DisplaySoundFragment newInstance() { 44 return new DisplaySoundFragment(); 45 } 46 getSoundEffectsEnabled(ContentResolver contentResolver)47 public static boolean getSoundEffectsEnabled(ContentResolver contentResolver) { 48 return Settings.System.getInt(contentResolver, Settings.System.SOUND_EFFECTS_ENABLED, 1) 49 != 0; 50 } 51 52 @Override onAttach(Context context)53 public void onAttach(Context context) { 54 mAudioManager = context.getSystemService(AudioManager.class); 55 super.onAttach(context); 56 } 57 58 @Override onCreatePreferences(Bundle savedInstanceState, String rootKey)59 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 60 setPreferencesFromResource(R.xml.display_sound, null); 61 62 final TwoStatePreference soundPref = findPreference(KEY_SOUND_EFFECTS); 63 soundPref.setChecked(getSoundEffectsEnabled()); 64 } 65 66 @Override onPreferenceTreeClick(Preference preference)67 public boolean onPreferenceTreeClick(Preference preference) { 68 if (TextUtils.equals(preference.getKey(), KEY_SOUND_EFFECTS)) { 69 final TwoStatePreference soundPref = (TwoStatePreference) preference; 70 setSoundEffectsEnabled(soundPref.isChecked()); 71 } 72 return super.onPreferenceTreeClick(preference); 73 } 74 getSoundEffectsEnabled()75 private boolean getSoundEffectsEnabled() { 76 return getSoundEffectsEnabled(getActivity().getContentResolver()); 77 } 78 setSoundEffectsEnabled(boolean enabled)79 private void setSoundEffectsEnabled(boolean enabled) { 80 if (enabled) { 81 mAudioManager.loadSoundEffects(); 82 } else { 83 mAudioManager.unloadSoundEffects(); 84 } 85 Settings.System.putInt(getActivity().getContentResolver(), 86 Settings.System.SOUND_EFFECTS_ENABLED, enabled ? 1 : 0); 87 } 88 89 @Override getMetricsCategory()90 public int getMetricsCategory() { 91 return 0; 92 } 93 } 94