Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a7999098 authored by Angela Wang's avatar Angela Wang Committed by Android (Google) Code Review
Browse files

Merge "Hearing Aid Compatibility toggle functionality in Settings"

parents 55753796 56409144
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -30,13 +30,12 @@
    <PreferenceCategory
        android:key="device_control_category"
        android:title="@string/accessibility_hearing_device_control">

        <SwitchPreference
            android:key="hearing_aid_compatibility"
            android:title="@string/accessibility_hac_mode_title"
            android:summary="@string/accessibility_hac_mode_summary"
            settings:searchable="true" />

            settings:searchable="true"
            settings:controller="com.android.settings.accessibility.HearingAidCompatibilityPreferenceController"/>
    </PreferenceCategory>

    <com.android.settings.accessibility.AccessibilityFooterPreference
+78 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.settings.accessibility;

import android.content.Context;
import android.media.AudioManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.core.TogglePreferenceController;

/** Preference controller for Hearing Aid Compatibility (HAC) settings */
public class HearingAidCompatibilityPreferenceController extends TogglePreferenceController {

    // Hearing Aid Compatibility settings values
    static final String HAC_KEY = "HACSetting";
    static final String HAC_VAL_ON = "ON";
    static final String HAC_VAL_OFF = "OFF";
    @VisibleForTesting
    static final int HAC_DISABLED = 0;
    @VisibleForTesting
    static final int HAC_ENABLED = 1;

    private final TelephonyManager mTelephonyManager;
    private final AudioManager mAudioManager;

    public HearingAidCompatibilityPreferenceController(Context context,
            String preferenceKey) {
        super(context, preferenceKey);
        mTelephonyManager = context.getSystemService(TelephonyManager.class);
        mAudioManager = context.getSystemService(AudioManager.class);
    }

    @Override
    public int getAvailabilityStatus() {
        return mTelephonyManager.isHearingAidCompatibilitySupported() ? AVAILABLE
                : UNSUPPORTED_ON_DEVICE;
    }

    @Override
    public boolean isChecked() {
        final int hac = Settings.System.getInt(mContext.getContentResolver(),
                Settings.System.HEARING_AID, HAC_DISABLED);
        return hac == HAC_ENABLED;
    }

    @Override
    public boolean setChecked(boolean isChecked) {
        setAudioParameterHacEnabled(isChecked);
        return Settings.System.putInt(mContext.getContentResolver(), Settings.System.HEARING_AID,
                (isChecked ? HAC_ENABLED : HAC_DISABLED));
    }

    @Override
    public int getSliceHighlightMenuRes() {
        return R.string.menu_key_accessibility;
    }

    private void setAudioParameterHacEnabled(boolean enabled) {
        mAudioManager.setParameters(HAC_KEY + "=" + (enabled ? HAC_VAL_ON : HAC_VAL_OFF));
    }
}
+16 −0
Original line number Diff line number Diff line
@@ -18,16 +18,23 @@ package com.android.settings.accessibility;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.telephony.TelephonyManager;

import androidx.test.core.app.ApplicationProvider;

import com.android.settings.R;
import com.android.settings.testutils.XmlTestUtils;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
@@ -40,7 +47,16 @@ public class AccessibilityHearingAidsFragmentTest {

    @Rule
    public MockitoRule mMockitoRule = MockitoJUnit.rule();
    @Spy
    private final Context mContext = ApplicationProvider.getApplicationContext();
    private TelephonyManager mTelephonyManager;

    @Before
    public void setUp() {
        mTelephonyManager = spy(mContext.getSystemService(TelephonyManager.class));
        when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
        doReturn(true).when(mTelephonyManager).isHearingAidCompatibilitySupported();
    }

    @Test
    public void getNonIndexableKeys_existInXmlLayout() {
+130 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.settings.accessibility;

import static com.android.settings.accessibility.HearingAidCompatibilityPreferenceController.HAC_DISABLED;
import static com.android.settings.accessibility.HearingAidCompatibilityPreferenceController.HAC_ENABLED;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;

/** Tests for {@link HearingAidCompatibilityPreferenceControllerTest}. */
@RunWith(RobolectricTestRunner.class)
public class HearingAidCompatibilityPreferenceControllerTest {

    @Rule
    public MockitoRule mocks = MockitoJUnit.rule();
    @Spy
    private final Context mContext = ApplicationProvider.getApplicationContext();
    @Mock
    private ContentResolver mContentResolver;

    private TelephonyManager mTelephonyManager;
    private AudioManager mAudioManager;
    private final SwitchPreference mPreference = new SwitchPreference(mContext);
    private HearingAidCompatibilityPreferenceController mController;

    @Before
    public void setUp() {
        mTelephonyManager = spy(mContext.getSystemService(TelephonyManager.class));
        mAudioManager = spy(mContext.getSystemService(AudioManager.class));
        when(mContext.getContentResolver()).thenReturn(mContentResolver);
        when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
        when(mContext.getSystemService(AudioManager.class)).thenReturn(mAudioManager);
        mController = new HearingAidCompatibilityPreferenceController(mContext,
                "hearing_aid_compatibility");
    }

    @Test
    public void getAvailabilityStatus_HacSupported_shouldReturnAvailable() {
        doReturn(true).when(mTelephonyManager).isHearingAidCompatibilitySupported();

        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
    }

    @Test
    public void getAvailabilityStatus_HacNotSupported_shouldReturnUnsupported() {
        doReturn(false).when(mTelephonyManager).isHearingAidCompatibilitySupported();

        assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
    }


    @Test
    public void isChecked_enabledHac_shouldReturnTrue() {
        Settings.System.putInt(mContext.getContentResolver(), Settings.System.HEARING_AID,
                HAC_ENABLED);
        mController.updateState(mPreference);

        assertThat(mController.isChecked()).isTrue();
        assertThat(mPreference.isChecked()).isTrue();
    }

    @Test
    public void isChecked_disabledHac_shouldReturnFalse() {
        Settings.System.putInt(mContext.getContentResolver(), Settings.System.HEARING_AID,
                HAC_DISABLED);
        mController.updateState(mPreference);

        assertThat(mController.isChecked()).isFalse();
        assertThat(mPreference.isChecked()).isFalse();
    }

    @Test
    public void setChecked_enabled_shouldEnableHac() {
        mController.setChecked(true);

        assertThat(Settings.System.getInt(mContext.getContentResolver(),
                Settings.System.HEARING_AID, HAC_DISABLED)).isEqualTo(HAC_ENABLED);
        verify(mAudioManager).setParameters("HACSetting=ON");
    }

    @Test
    public void setChecked_disabled_shouldDisableHac() {
        mController.setChecked(false);

        assertThat(Settings.System.getInt(mContext.getContentResolver(),
                Settings.System.HEARING_AID, HAC_DISABLED)).isEqualTo(HAC_DISABLED);
        verify(mAudioManager).setParameters("HACSetting=OFF");
    }

}