Home | History | Annotate | Download | only in sound
      1 /*
      2  * Copyright (C) 2014 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.app.Activity;
     20 import android.content.ContentResolver;
     21 import android.content.Context;
     22 import android.media.AudioManager;
     23 import android.net.Uri;
     24 import android.os.Bundle;
     25 import android.provider.Settings;
     26 import com.android.tv.settings.dialog.DialogFragment;
     27 import com.android.tv.settings.dialog.DialogFragment.Action;
     28 
     29 import com.android.tv.settings.BrowseInfo;
     30 import com.android.tv.settings.R;
     31 
     32 import java.util.ArrayList;
     33 
     34 /**
     35  * Activity that allows the enabling and disabling of sound effects.
     36  */
     37 public class SoundActivity extends Activity implements Action.Listener {
     38 
     39     private static final String PREFERENCE_KEY = "sound_effects";
     40     private static final String ACTION_SOUND_ON = "sound_on";
     41     private static final String ACTION_SOUND_OFF = "sound_off";
     42     private AudioManager mAudioManager;
     43     private DialogFragment mDialogFragment;
     44 
     45     @Override
     46     protected void onCreate(Bundle savedInstanceState) {
     47         super.onCreate(savedInstanceState);
     48         mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
     49 
     50         mDialogFragment = new DialogFragment.Builder()
     51                 .title(getString(R.string.device_sound_effects))
     52                 .breadcrumb(getString(R.string.header_category_device))
     53                 .iconResourceId(getIconResource(getContentResolver()))
     54                 .iconBackgroundColor(getResources().getColor(R.color.icon_background))
     55                 .actions(getActions()).build();
     56         DialogFragment.add(getFragmentManager(), mDialogFragment);
     57     }
     58 
     59     private ArrayList<Action> getActions() {
     60         boolean soundEffectsAreOn = getSoundEffectsEnabled(getContentResolver());
     61         ArrayList<Action> actions = new ArrayList<Action>();
     62         actions.add(new Action.Builder()
     63                 .key(ACTION_SOUND_ON)
     64                 .title(getString(R.string.settings_on))
     65                 .checked(soundEffectsAreOn)
     66                 .checkSetId(1)
     67                 .build());
     68         actions.add(new Action.Builder()
     69                 .key(ACTION_SOUND_OFF)
     70                 .title(getString(R.string.settings_off))
     71                 .checked(!soundEffectsAreOn)
     72                 .checkSetId(1)
     73                 .build());
     74         return actions;
     75     }
     76 
     77     @Override
     78     public void onActionClicked(Action action) {
     79         if (ACTION_SOUND_ON.equals(action.getKey())) {
     80             mAudioManager.loadSoundEffects();
     81             setSoundEffectsEnabled(1);
     82         } else if (ACTION_SOUND_OFF.equals(action.getKey())) {
     83             mAudioManager.unloadSoundEffects();
     84             setSoundEffectsEnabled(0);
     85         }
     86         mDialogFragment.setIcon(getIconResource(getContentResolver()));
     87     }
     88 
     89     public static String getPreferenceKey() {
     90         return PREFERENCE_KEY;
     91     }
     92 
     93     public static int getIconResource(ContentResolver contentResolver) {
     94         return getSoundEffectsEnabled(contentResolver) ? R.drawable.ic_settings_sound_on
     95                 : R.drawable.ic_settings_sound_off;
     96     }
     97 
     98     private void setSoundEffectsEnabled(int value) {
     99         Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED, value);
    100     }
    101 
    102     private static boolean getSoundEffectsEnabled(ContentResolver contentResolver) {
    103         return Settings.System.getInt(contentResolver, Settings.System.SOUND_EFFECTS_ENABLED, 1)
    104                 != 0;
    105     }
    106 }
    107