• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.R;
26 import com.android.settings.core.TogglePreferenceController;
27 import com.android.settings.overlay.FeatureFactory;
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         try {
54             return mTelephonyManager.isHearingAidCompatibilitySupported() ? AVAILABLE
55                     : UNSUPPORTED_ON_DEVICE;
56         } catch (UnsupportedOperationException e) {
57             // Device doesn't support FEATURE_TELEPHONY_CALLING
58             return UNSUPPORTED_ON_DEVICE;
59         }
60     }
61 
62     @Override
isChecked()63     public boolean isChecked() {
64         final int hac = Settings.System.getInt(mContext.getContentResolver(),
65                 Settings.System.HEARING_AID, HAC_DISABLED);
66         return hac == HAC_ENABLED;
67     }
68 
69     @Override
setChecked(boolean isChecked)70     public boolean setChecked(boolean isChecked) {
71         FeatureFactory.getFeatureFactory().getMetricsFeatureProvider().changed(
72                 getMetricsCategory(), getPreferenceKey(), isChecked ? 1 : 0);
73         setAudioParameterHacEnabled(isChecked);
74         return Settings.System.putInt(mContext.getContentResolver(), Settings.System.HEARING_AID,
75                 (isChecked ? HAC_ENABLED : HAC_DISABLED));
76     }
77 
78     @Override
getSliceHighlightMenuRes()79     public int getSliceHighlightMenuRes() {
80         return R.string.menu_key_accessibility;
81     }
82 
setAudioParameterHacEnabled(boolean enabled)83     private void setAudioParameterHacEnabled(boolean enabled) {
84         mAudioManager.setParameters(HAC_KEY + "=" + (enabled ? HAC_VAL_ON : HAC_VAL_OFF) + ";");
85     }
86 }
87