summaryrefslogtreecommitdiff
path: root/Settings/src/com/android/tv/settings/device/displaysound/SoundFormatPreferenceController.java
blob: 2cf6f73998d055f1dc9c8e616b46e8c9670957f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.settings.device.displaysound;

import android.annotation.NonNull;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

import androidx.preference.Preference;
import androidx.preference.SwitchPreference;

import com.android.settingslib.core.AbstractPreferenceController;
import com.android.tv.settings.R;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * Controller for the surround sound switch preferences.
 */
public class SoundFormatPreferenceController extends AbstractPreferenceController {

    private static final String TAG = "SoundFormatController";

    private int mFormatId;
    private Map<Integer, Boolean> mFormats;
    private List<Integer> mReportedFormats;
    private AudioManager mAudioManager;

    public SoundFormatPreferenceController(
            Context context,
            int formatId,
            AudioManager audioManager,
            @NonNull Map<Integer, Boolean> formats,
            @NonNull List<Integer> reportedFormats) {
        super(context);
        mFormatId = formatId;
        mAudioManager = audioManager;
        mFormats = formats;
        mReportedFormats = reportedFormats;
    }

    @Override
    public boolean isAvailable() {
        return true;
    }

    @Override
    public String getPreferenceKey() {
        return AdvancedVolumeFragment.KEY_SURROUND_SOUND_FORMAT_PREFIX + mFormatId;
    }

    @Override
    public void updateState(Preference preference) {
        super.updateState(preference);
        if (preference.getKey().equals(getPreferenceKey())) {
            preference.setEnabled(getFormatPreferencesEnabledState());
            ((SwitchPreference) preference).setChecked(getFormatPreferenceCheckedState());
        }
    }

    @Override
    public boolean handlePreferenceTreeClick(Preference preference) {
        if (preference.getKey().equals(getPreferenceKey())) {
            onPreferenceClicked((SwitchPreference) preference);
        }
        return super.handlePreferenceTreeClick(preference);
    }

    /**
     * @return checked state of a surround sound format switch based on passthrough setting and
     * audio manager state for the format.
     */
    private boolean getFormatPreferenceCheckedState() {
        switch (AdvancedVolumeFragment.getSurroundPassthroughSetting(mContext)) {
            case AdvancedVolumeFragment.KEY_SURROUND_SOUND_AUTO:
                return isReportedFormat();
            case AdvancedVolumeFragment.KEY_SURROUND_SOUND_MANUAL:
                return getEnabledFormats().contains(mFormatId);
            default:
                return false;
        }
    }

    /** @return true if the format checkboxes should be enabled, i.e. in manual mode. */
    private boolean getFormatPreferencesEnabledState() {
        return AdvancedVolumeFragment.getSurroundPassthroughSetting(mContext)
                == AdvancedVolumeFragment.KEY_SURROUND_SOUND_MANUAL;
    }

    /** @return the formats that are enabled in global settings */
    HashSet<Integer> getEnabledFormats() {
        HashSet<Integer> formats = new HashSet<>();
        String enabledFormats = Settings.Global.getString(mContext.getContentResolver(),
                Settings.Global.ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS);
        if (enabledFormats == null) {
            // Starting with Android P passthrough setting ALWAYS has been replaced with MANUAL.
            // In that case all formats will be enabled when in MANUAL mode.
            formats.addAll(mFormats.keySet());
        } else {
            try {
                Arrays.stream(TextUtils.split(enabledFormats, ",")).mapToInt(Integer::parseInt)
                        .forEach(formats::add);
            } catch (NumberFormatException e) {
                Log.w(TAG, "ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS misformatted.", e);
            }
        }
        return formats;
    }

    /**
     * Handler for when this particular format preference is clicked.
     */
    private void onPreferenceClicked(SwitchPreference preference) {
        final boolean enabled = preference.isChecked();
        // In case of enabling unsupported format, show a warning dialog
        if (!isReportedFormat() && enabled) {
            showWarningDialogOnEnableUnsupportedFormat(preference);
        } else {
            mAudioManager.setSurroundFormatEnabled(mFormatId, enabled);
        }
    }

    /** @return true if the given format is reported by the device. */
    private boolean isReportedFormat() {
        return mReportedFormats.contains(mFormatId);
    }

    private void showWarningDialogOnEnableUnsupportedFormat(SwitchPreference preference) {
        new AlertDialog.Builder(mContext)
            .setTitle(R.string.surround_sound_enable_unsupported_dialog_title)
            .setMessage(R.string.surround_sound_enable_unsupported_dialog_desc)
            .setPositiveButton(
                    R.string.surround_sound_enable_unsupported_dialog_ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            mAudioManager.setSurroundFormatEnabled(mFormatId, true);
                            dialog.dismiss();
                        }
                    })
            .setNegativeButton(
                    R.string.surround_sound_enable_unsupported_dialog_cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            preference.setChecked(false);
                            dialog.dismiss();
                        }
                    })
            .show();
    }
}