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