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

Commit 7a48cb47 authored by Antony Sargent's avatar Antony Sargent Committed by android-build-merger
Browse files

Add a "data during calls" preference am: aeb1da0d

am: 7f7f7cda

Change-Id: Iaafabc24ace39911ab3198cd705311595832cb5d
parents 64dad09e 7f7f7cda
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -85,6 +85,12 @@
            android:summary="@string/mms_message_summary"
            settings:controller="com.android.settings.network.telephony.MmsMessagePreferenceController"/>

        <SwitchPreference
            android:key="data_during_calls"
            android:title="@string/data_during_calls_title"
            android:summary="@string/data_during_calls_summary"
            settings:controller="com.android.settings.network.telephony.DataDuringCallsPreferenceController"/>

        <SwitchPreference
            android:key="enhanced_4g_lte"
            android:title="@string/enhanced_4g_lte_mode_title"
+104 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.network.telephony;

import static androidx.lifecycle.Lifecycle.Event.ON_PAUSE;
import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;

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

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;

import com.android.settings.network.SubscriptionsChangeListener;

public class DataDuringCallsPreferenceController extends TelephonyTogglePreferenceController
        implements LifecycleObserver,
        SubscriptionsChangeListener.SubscriptionsChangeListenerClient {

    private SwitchPreference mPreference;
    private SubscriptionsChangeListener mChangeListener;
    private TelephonyManager mManager;

    public DataDuringCallsPreferenceController(Context context,
            String preferenceKey) {
        super(context, preferenceKey);
        mChangeListener = new SubscriptionsChangeListener(mContext, this);
    }

    public void init(Lifecycle lifecycle, int subId) {
        this.mSubId = subId;
        mManager = mContext.getSystemService(TelephonyManager.class).createForSubscriptionId(subId);
        lifecycle.addObserver(this);
    }

    @OnLifecycleEvent(ON_RESUME)
    public void onResume() {
        mChangeListener.start();
    }

    @OnLifecycleEvent(ON_PAUSE)
    public void onPause() {
        mChangeListener.stop();
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mPreference = screen.findPreference(getPreferenceKey());
    }

    @Override
    public boolean isChecked() {
        return mManager.isDataAllowedInVoiceCall();
    }

    @Override
    public boolean setChecked(boolean isChecked) {
        mManager.setDataAllowedDuringVoiceCall(isChecked);
        return true;
    }

    @Override
    public int getAvailabilityStatus(int subId) {
        if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID ||
                SubscriptionManager.getDefaultDataSubscriptionId() == mSubId) {
            return CONDITIONALLY_UNAVAILABLE;
        }
        return AVAILABLE;
    }

    @Override
    public void updateState(Preference preference) {
        super.updateState(preference);
        preference.setVisible(isAvailable());
    }

    @Override
    public void onAirplaneModeChanged(boolean airplaneModeEnabled) {}

    @Override
    public void onSubscriptionsChanged() {
        updateState(mPreference);
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -138,6 +138,7 @@ public class MobileNetworkSettings extends RestrictedDashboardFragment {
            use(CarrierSettingsVersionPreferenceController.class).init(mSubId);
            use(BillingCyclePreferenceController.class).init(mSubId);
            use(MmsMessagePreferenceController.class).init(mSubId);
            use(DataDuringCallsPreferenceController.class).init(getLifecycle(), mSubId);
            use(DisabledSubscriptionController.class).init(getLifecycle(), mSubId);
            use(DeleteSimProfilePreferenceController.class).init(mSubId, this,
                    REQUEST_CODE_DELETE_SUBSCRIPTION);
+1 −0
Original line number Diff line number Diff line
@@ -4,3 +4,4 @@ com.android.settings.testutils.FakeInvalidSliderController
com.android.settings.core.TogglePreferenceControllerTest$FakeToggle
com.android.settings.accessibility.AccessibilitySlicePreferenceController
com.android.settings.network.telephony.MmsMessagePreferenceController
com.android.settings.network.telephony.DataDuringCallsPreferenceController
+134 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.network;

import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;

import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;

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

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
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.lifecycle.Lifecycle;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;

import com.android.settings.network.telephony.DataDuringCallsPreferenceController;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowSubscriptionManager;

@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowSubscriptionManager.class)
public class DataDuringCallsPreferenceControllerTest {
    private static final String PREF_KEY = "pref_key";
    private static final int SUB_ID_1 = 111;
    private static final int SUB_ID_2 = 222;

    @Mock
    private TelephonyManager mTelephonyManager;
    @Mock
    private Lifecycle mLifecycle;
    @Mock
    private PreferenceScreen mPreferenceScreen;

    private Context mContext;
    private SwitchPreference mSwitchPreference;
    private DataDuringCallsPreferenceController mController;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mContext = spy(RuntimeEnvironment.application);
        doReturn(mTelephonyManager).when(mContext).getSystemService(eq(TelephonyManager.class));
        when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mTelephonyManager);
        mSwitchPreference = new SwitchPreference(mContext);
        when(mPreferenceScreen.findPreference(PREF_KEY)).thenReturn(mSwitchPreference);
        mController = new DataDuringCallsPreferenceController(mContext, PREF_KEY);
        mController.init(mLifecycle, SUB_ID_1);
    }

    @Test
    public void getAvailabilityStatus_noInit_notAvailable() {
        ShadowSubscriptionManager.setDefaultDataSubscriptionId(SUB_ID_1);
        DataDuringCallsPreferenceController controller =
                new DataDuringCallsPreferenceController(mContext, PREF_KEY);

        // note that we purposely don't call init first on the controller
        assertThat(controller.getAvailabilityStatus(INVALID_SUBSCRIPTION_ID)).isEqualTo(
                CONDITIONALLY_UNAVAILABLE);
    }

    @Test
    public void displayPreference_defaultForData_notAvailable() {
        ShadowSubscriptionManager.setDefaultDataSubscriptionId(SUB_ID_1);

        mController.displayPreference(mPreferenceScreen);

        assertThat(mController.isAvailable()).isFalse();
        assertThat(mSwitchPreference.isVisible()).isFalse();
    }

    @Test
    public void  displayPreference_notDefaultForData_available() {
        ShadowSubscriptionManager.setDefaultDataSubscriptionId(SUB_ID_2);

        mController.displayPreference(mPreferenceScreen);

        assertThat(mController.isAvailable()).isTrue();
        assertThat(mSwitchPreference.isVisible()).isTrue();
    }

    @Test
    public void onSubscriptionsChanged_becomesDefaultForData_notAvailable() {
        ShadowSubscriptionManager.setDefaultDataSubscriptionId(SUB_ID_2);

        mController.displayPreference(mPreferenceScreen);
        ShadowSubscriptionManager.setDefaultDataSubscriptionId(SUB_ID_1);
        mController.onSubscriptionsChanged();

        assertThat(mController.isAvailable()).isFalse();
        assertThat(mSwitchPreference.isVisible()).isFalse();
    }

    @Test
    public void onSubscriptionsChanged_noLongerDefaultForData_available() {
        ShadowSubscriptionManager.setDefaultDataSubscriptionId(SUB_ID_1);

        mController.displayPreference(mPreferenceScreen);
        ShadowSubscriptionManager.setDefaultDataSubscriptionId(SUB_ID_2);
        mController.onSubscriptionsChanged();

        assertThat(mController.isAvailable()).isTrue();
        assertThat(mSwitchPreference.isVisible()).isTrue();
    }
}