1 /*
2  * Copyright (C) 2022 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.settings.accessibility;
18 
19 import android.content.Context;
20 import android.media.AudioManager;
21 import android.provider.Settings;
22 import android.telephony.TelephonyManager;
23 
24 import com.android.internal.annotations.VisibleForTesting;
25 import com.android.internal.telephony.flags.Flags;
26 import com.android.settings.R;
27 import com.android.settings.core.TogglePreferenceController;
28 
29 /** Preference controller for Hearing Aid Compatibility (HAC) settings */
30 public class HearingAidCompatibilityPreferenceController extends TogglePreferenceController {
31 
32     // Hearing Aid Compatibility settings values
33     static final String HAC_KEY = "HACSetting";
34     static final String HAC_VAL_ON = "ON";
35     static final String HAC_VAL_OFF = "OFF";
36     @VisibleForTesting
37     static final int HAC_DISABLED = 0;
38     @VisibleForTesting
39     static final int HAC_ENABLED = 1;
40 
41     private final TelephonyManager mTelephonyManager;
42     private final AudioManager mAudioManager;
43 
HearingAidCompatibilityPreferenceController(Context context, String preferenceKey)44     public HearingAidCompatibilityPreferenceController(Context context,
45             String preferenceKey) {
46         super(context, preferenceKey);
47         mTelephonyManager = context.getSystemService(TelephonyManager.class);
48         mAudioManager = context.getSystemService(AudioManager.class);
49     }
50 
51     @Override
getAvailabilityStatus()52     public int getAvailabilityStatus() {
53         if (Flags.enforceTelephonyFeatureMappingForPublicApis()) {
54             try {
55                 return mTelephonyManager.isHearingAidCompatibilitySupported() ? AVAILABLE
56                         : UNSUPPORTED_ON_DEVICE;
57             } catch (UnsupportedOperationException e) {
58                 // Device doesn't support FEATURE_TELEPHONY_CALLING
59                 return UNSUPPORTED_ON_DEVICE;
60             }
61         } else {
62             return mTelephonyManager.isHearingAidCompatibilitySupported() ? AVAILABLE
63                     : UNSUPPORTED_ON_DEVICE;
64         }
65     }
66 
67     @Override
isChecked()68     public boolean isChecked() {
69         final int hac = Settings.System.getInt(mContext.getContentResolver(),
70                 Settings.System.HEARING_AID, HAC_DISABLED);
71         return hac == HAC_ENABLED;
72     }
73 
74     @Override
setChecked(boolean isChecked)75     public boolean setChecked(boolean isChecked) {
76         setAudioParameterHacEnabled(isChecked);
77         return Settings.System.putInt(mContext.getContentResolver(), Settings.System.HEARING_AID,
78                 (isChecked ? HAC_ENABLED : HAC_DISABLED));
79     }
80 
81     @Override
getSliceHighlightMenuRes()82     public int getSliceHighlightMenuRes() {
83         return R.string.menu_key_accessibility;
84     }
85 
setAudioParameterHacEnabled(boolean enabled)86     private void setAudioParameterHacEnabled(boolean enabled) {
87         mAudioManager.setParameters(HAC_KEY + "=" + (enabled ? HAC_VAL_ON : HAC_VAL_OFF) + ";");
88     }
89 }
90