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

Commit 2c03c38c authored by Aleksander Morgado's avatar Aleksander Morgado Committed by Android (Google) Code Review
Browse files

Merge changes from topic "voice-check-refactor" into main

* changes:
  Check voice support using TelephonyManager.isDeviceVoiceCapable()
  Hide voice call related settings with config_show_sim_info=false
  Prefer mocking TelephonyManager in PowerButtonEndsCallPreferenceControllerTest
parents dc71f9f3 fe22c257
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -739,7 +739,7 @@
    <!-- Whether to enable the app battery usage list page feature. -->
    <bool name="config_app_battery_usage_list_enabled">false</bool>

    <!-- Whether sim related information is visible to the end user. -->
    <!-- Whether telephony related information is visible to the end user. -->
    <bool name="config_show_sim_info">true</bool>

    <!-- In the case of receiving both help and progress message, display progress message. -->
+10 −1
Original line number Diff line number Diff line
@@ -214,9 +214,18 @@ public final class Utils extends com.android.settingslib.Utils {
     * Returns whether the device is voice-capable (meaning, it is also a phone).
     */
    public static boolean isVoiceCapable(Context context) {
        if (isTelephonyDisabled(context)) return false;
        final TelephonyManager telephony =
                (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return telephony != null && telephony.isVoiceCapable();
        return telephony != null && telephony.isDeviceVoiceCapable();
    }

    /**
     * Returns whether telephony features are completely disabled in the app, regardless
     * of the TelephonyManager reported capabilities or the PackageManager flags declared.
     */
    private static boolean isTelephonyDisabled(Context context) {
        return !context.getResources().getBoolean(R.bool.config_show_sim_info);
    }

    /**
+21 −4
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;

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

import androidx.test.core.app.ApplicationProvider;
@@ -53,6 +54,9 @@ public class HearingDeviceCallRoutingPreferenceControllerTest {
    private final Context mContext = ApplicationProvider.getApplicationContext();
    private static final String FAKE_KEY = "fake_key";

    @Spy
    private final Resources mResources = mContext.getResources();

    @Mock
    private TelephonyManager mTelephonyManager;
    private HearingDeviceCallRoutingPreferenceController mController;
@@ -60,20 +64,33 @@ public class HearingDeviceCallRoutingPreferenceControllerTest {
    @Before
    public void setUp() {
        when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
        when(mContext.getResources()).thenReturn(mResources);
        mController = new HearingDeviceCallRoutingPreferenceController(mContext, FAKE_KEY);
    }

    @Test
    public void getAvailabilityStatus_hasTelephonyCalling_available() {
        when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
    public void getAvailabilityStatus_telephonyEnabled_voiceCapable_available() {
        when(mResources.getBoolean(com.android.settings.R.bool.config_show_sim_info))
                .thenReturn(true);
        when(mTelephonyManager.isDeviceVoiceCapable()).thenReturn(true);

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

    @Test
    public void getAvailabilityStatus_noTelephonyCalling_unsupported() {
        when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
    public void getAvailabilityStatus_telephonyEnabled_notVoiceCapable_unsupported() {
        when(mResources.getBoolean(com.android.settings.R.bool.config_show_sim_info))
                .thenReturn(true);
        when(mTelephonyManager.isDeviceVoiceCapable()).thenReturn(false);

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

    @Test
    public void getAvailabilityStatus_telephonyDisabled_voiceCapable_unsupported() {
        when(mResources.getBoolean(com.android.settings.R.bool.config_show_sim_info))
                .thenReturn(false);
        when(mTelephonyManager.isDeviceVoiceCapable()).thenReturn(true);

        assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
    }
+45 −9
Original line number Diff line number Diff line
@@ -18,50 +18,71 @@ package com.android.settings.accessibility;

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

import static org.mockito.Mockito.when;

import android.content.Context;
import android.content.res.Resources;
import android.provider.Settings;
import android.telephony.TelephonyManager;

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

import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.shadow.ShadowKeyCharacterMap;
import com.android.settings.testutils.shadow.ShadowUtils;

import org.junit.After;
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;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowUtils.class, ShadowKeyCharacterMap.class})
@Config(shadows = {ShadowKeyCharacterMap.class})
public class PowerButtonEndsCallPreferenceControllerTest {

    private static final int UNKNOWN = -1;

    private Context mContext;
    @Rule
    public MockitoRule mMockitoRule = MockitoJUnit.rule();

    @Mock
    private TelephonyManager mTelephonyManager;

    @Spy
    private final Context mContext = ApplicationProvider.getApplicationContext();

    @Spy
    private final Resources mResources = mContext.getResources();

    private SwitchPreference mPreference;
    private PowerButtonEndsCallPreferenceController mController;

    @Before
    public void setUp() {
        mContext = RuntimeEnvironment.application;
        when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
        when(mContext.getResources()).thenReturn(mResources);
        mPreference = new SwitchPreference(mContext);
        mController = new PowerButtonEndsCallPreferenceController(mContext, "power_button");
    }

    @After
    public void tearDown() {
        ShadowUtils.reset();
        ShadowKeyCharacterMap.reset();
    }

    @Test
    public void getAvailabilityStatus_hasPowerKeyAndVoiceCapable_shouldReturnAvailable() {
        ShadowKeyCharacterMap.setDevicehasKey(true);
        ShadowUtils.setIsVoiceCapable(true);
        when(mResources.getBoolean(com.android.settings.R.bool.config_show_sim_info))
                .thenReturn(true);
        when(mTelephonyManager.isDeviceVoiceCapable()).thenReturn(true);

        assertThat(mController.getAvailabilityStatus())
                .isEqualTo(BasePreferenceController.AVAILABLE);
@@ -70,7 +91,20 @@ public class PowerButtonEndsCallPreferenceControllerTest {
    @Test
    public void getAvailabilityStatus_noVoiceCapable_shouldReturnUnsupportedOnDevice() {
        ShadowKeyCharacterMap.setDevicehasKey(true);
        ShadowUtils.setIsVoiceCapable(false);
        when(mResources.getBoolean(com.android.settings.R.bool.config_show_sim_info))
                .thenReturn(true);
        when(mTelephonyManager.isDeviceVoiceCapable()).thenReturn(false);

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

    @Test
    public void getAvailabilityStatus_telephonyDisabled_shouldReturnUnsupportedOnDevice() {
        ShadowKeyCharacterMap.setDevicehasKey(true);
        when(mResources.getBoolean(com.android.settings.R.bool.config_show_sim_info))
                .thenReturn(false);
        when(mTelephonyManager.isDeviceVoiceCapable()).thenReturn(true);

        assertThat(mController.getAvailabilityStatus())
                .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
@@ -79,7 +113,9 @@ public class PowerButtonEndsCallPreferenceControllerTest {
    @Test
    public void getAvailabilityStatus_noPowerKey_shouldReturnUnsupportedOnDevice() {
        ShadowKeyCharacterMap.setDevicehasKey(false);
        ShadowUtils.setIsVoiceCapable(true);
        when(mResources.getBoolean(com.android.settings.R.bool.config_show_sim_info))
                .thenReturn(true);
        when(mTelephonyManager.isDeviceVoiceCapable()).thenReturn(true);

        assertThat(mController.getAvailabilityStatus())
                .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
+23 −3
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.content.res.Resources;
import android.media.AudioManager;
import android.os.Vibrator;
import android.provider.Settings;
@@ -59,6 +60,7 @@ public class VibrationRampingRingerTogglePreferenceControllerTest {

    private Lifecycle mLifecycle;
    private Context mContext;
    private Resources mResources;
    private VibrationRampingRingerTogglePreferenceController mController;
    private SwitchPreference mPreference;

@@ -67,6 +69,8 @@ public class VibrationRampingRingerTogglePreferenceControllerTest {
        MockitoAnnotations.initMocks(this);
        mLifecycle = new Lifecycle(() -> mLifecycle);
        mContext = spy(ApplicationProvider.getApplicationContext());
        mResources = spy(mContext.getResources());
        when(mContext.getResources()).thenReturn(mResources);
        when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
        when(mContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mAudioManager);
        when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL);
@@ -86,7 +90,19 @@ public class VibrationRampingRingerTogglePreferenceControllerTest {

    @Test
    public void getAvailabilityStatus_notVoiceCapable_returnUnsupportedOnDevice() {
        when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
        when(mResources.getBoolean(com.android.settings.R.bool.config_show_sim_info))
                .thenReturn(true);
        when(mTelephonyManager.isDeviceVoiceCapable()).thenReturn(false);
        when(mDeviceConfigProvider.isRampingRingerEnabledOnTelephonyConfig()).thenReturn(false);

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

    @Test
    public void getAvailabilityStatus_telephonyDisabled_returnUnsupportedOnDevice() {
        when(mResources.getBoolean(com.android.settings.R.bool.config_show_sim_info))
                .thenReturn(false);
        when(mTelephonyManager.isDeviceVoiceCapable()).thenReturn(true);
        when(mDeviceConfigProvider.isRampingRingerEnabledOnTelephonyConfig()).thenReturn(false);

        assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
@@ -94,7 +110,9 @@ public class VibrationRampingRingerTogglePreferenceControllerTest {

    @Test
    public void getAvailabilityStatus_rampingRingerEnabled_returnUnsupportedOnDevice() {
        when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
        when(mResources.getBoolean(com.android.settings.R.bool.config_show_sim_info))
                .thenReturn(true);
        when(mTelephonyManager.isDeviceVoiceCapable()).thenReturn(true);
        when(mDeviceConfigProvider.isRampingRingerEnabledOnTelephonyConfig()).thenReturn(true);

        assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
@@ -102,7 +120,9 @@ public class VibrationRampingRingerTogglePreferenceControllerTest {

    @Test
    public void getAvailabilityStatus_voiceCapableAndRampingRingerDisabled_returnAvailable() {
        when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
        when(mResources.getBoolean(com.android.settings.R.bool.config_show_sim_info))
                .thenReturn(true);
        when(mTelephonyManager.isDeviceVoiceCapable()).thenReturn(true);
        when(mDeviceConfigProvider.isRampingRingerEnabledOnTelephonyConfig()).thenReturn(false);

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