1 /*
2  * Copyright (C) 2021 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.widget;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 
22 import androidx.preference.PreferenceViewHolder;
23 import androidx.preference.SwitchPreference;
24 
25 import com.android.tv.settings.SystemSoundsPlayer;
26 import com.android.tv.settings.TvSettingsApplication;
27 
28 /**
29  * SwitchPreference that plays custom select / deselect sound effects
30  */
31 public class SwitchWithSoundPreference extends SwitchPreference {
32     private final SystemSoundsPlayer mSystemSoundsPlayer;
33 
SwitchWithSoundPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)34     public SwitchWithSoundPreference(Context context, AttributeSet attrs, int defStyleAttr,
35             int defStyleRes) {
36         super(context, attrs, defStyleAttr, defStyleRes);
37         TvSettingsApplication app = (TvSettingsApplication) context.getApplicationContext();
38         mSystemSoundsPlayer = app.getSystemSoundsPlayer();
39     }
40 
41 
SwitchWithSoundPreference(Context context, AttributeSet attrs)42     public SwitchWithSoundPreference(Context context, AttributeSet attrs) {
43         super(context, attrs);
44         TvSettingsApplication app = (TvSettingsApplication) context.getApplicationContext();
45         mSystemSoundsPlayer = app.getSystemSoundsPlayer();
46     }
47 
SwitchWithSoundPreference(Context context)48     public SwitchWithSoundPreference(Context context) {
49         super(context);
50         TvSettingsApplication app = (TvSettingsApplication) context.getApplicationContext();
51         mSystemSoundsPlayer = app.getSystemSoundsPlayer();
52     }
53 
54     @Override
onBindViewHolder(PreferenceViewHolder holder)55     public void onBindViewHolder(PreferenceViewHolder holder) {
56         super.onBindViewHolder(holder);
57         if (mSystemSoundsPlayer == null) {
58             return;
59         }
60         // disable default click sound effect played by android.view.View#performClick()
61         holder.itemView.setSoundEffectsEnabled(false);
62     }
63 
64     @Override
onClick()65     protected void onClick() {
66         boolean checkedBeforeClick = isChecked();
67         super.onClick();
68         if (mSystemSoundsPlayer == null) {
69             return;
70         }
71         if (isChecked() != checkedBeforeClick) {
72             if (isChecked()) {
73                 mSystemSoundsPlayer.playSoundEffect(SystemSoundsPlayer.FX_SELECT);
74             } else {
75                 mSystemSoundsPlayer.playSoundEffect(SystemSoundsPlayer.FX_DESELECT);
76             }
77         }
78     }
79 }
80