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

Commit ee41716c authored by Sarah Chin's avatar Sarah Chin
Browse files

Combine data roaming from user action into one preference

Instead of using DATA_ROAMING_IS_USER_SETTING_KEY for SSSS and
DATA_ROAMING + subId for DSDS, combine both into
DATA_ROAMING_IS_USER_SETTING_KEY + subId.

Test: verify data roaming settings persisted across reboots
Test: verify data roaming settings persisted across upgrades
Test: verify data roaming settings correct based on carrier configs
Test: atest DataSettingsManagerTest
Bug: 259604889
Change-Id: Id73e66b2fd96a367ff469b464bf01bedddf6fdd3
Merged-In: Id73e66b2fd96a367ff469b464bf01bedddf6fdd3
parent b64d6027
Loading
Loading
Loading
Loading
+28 −31
Original line number Diff line number Diff line
@@ -544,42 +544,37 @@ public class DataSettingsManager extends Handler {
     * has not manually set the value. The default value is {@link #isDefaultDataRoamingEnabled()}.
     */
    public void setDefaultDataRoamingEnabled() {
        // For SSSS, this is a per-phone property from DATA_ROAMING_IS_USER_SETTING_KEY.
        // For DSDS, this is a per-sub property from Settings.Global.DATA_ROAMING + subId.
        // If the user has not manually set the value, use the default value.
        boolean useCarrierSpecificDefault = false;
        if (mPhone.getContext().getSystemService(TelephonyManager.class).getSimCount() != 1) {
            String setting = Settings.Global.DATA_ROAMING + mPhone.getSubId();
            try {
                Settings.Global.getInt(mResolver, setting);
            } catch (Settings.SettingNotFoundException ex) {
                // For multi-SIM phones, use the default value if uninitialized.
                useCarrierSpecificDefault = true;
            }
        } else if (!isDataRoamingFromUserAction()) {
            // For single-SIM phones, use the default value if user action is not set.
            useCarrierSpecificDefault = true;
        }
        log("setDefaultDataRoamingEnabled: useCarrierSpecificDefault=" + useCarrierSpecificDefault);
        if (useCarrierSpecificDefault) {
            boolean defaultVal = isDefaultDataRoamingEnabled();
            setDataRoamingEnabledInternal(defaultVal);
        if (!isDataRoamingFromUserAction()) {
            setDataRoamingEnabledInternal(isDefaultDataRoamingEnabled());
        }
    }

    /**
     * Get whether the user has manually enabled or disabled data roaming from settings.
     * @return {@code true} if the user has enabled data roaming and {@code false} if they have not.
     * Get whether the user has manually enabled or disabled data roaming from settings for the
     * current subscription.
     * @return {@code true} if the user has manually enabled data roaming for the current
     *         subscription and {@code false} if they have not.
     */
    private boolean isDataRoamingFromUserAction() {
        final SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(mPhone.getContext());
        // Since we don't want to unset user preferences after a system update, default to true if
        // the preference does not exist and set it to false explicitly from factory reset.
        if (!sp.contains(Phone.DATA_ROAMING_IS_USER_SETTING_KEY)) {
            sp.edit().putBoolean(Phone.DATA_ROAMING_IS_USER_SETTING_KEY, false).commit();
        String key = Phone.DATA_ROAMING_IS_USER_SETTING_KEY + mPhone.getSubId();
        final SharedPreferences sp =
                PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());

        // Set the default roaming from user action value if the preference doesn't exist
        if (!sp.contains(key)) {
            if (sp.contains(Phone.DATA_ROAMING_IS_USER_SETTING_KEY)) {
                log("Reusing previous roaming from user action value for backwards compatibility.");
                sp.edit().putBoolean(key, true).commit();
            } else {
                log("Clearing roaming from user action value for new or upgrading devices.");
                sp.edit().putBoolean(key, false).commit();
            }
        return sp.getBoolean(Phone.DATA_ROAMING_IS_USER_SETTING_KEY, true);
        }

        boolean isUserSetting = sp.getBoolean(key, true);
        log("isDataRoamingFromUserAction: key=" + key + ", isUserSetting=" + isUserSetting);
        return isUserSetting;
    }

    /**
@@ -588,9 +583,11 @@ public class DataSettingsManager extends Handler {
     * {@link #isDefaultDataRoamingEnabled()} will continue to be used.
     */
    private void setDataRoamingFromUserAction() {
        final SharedPreferences.Editor sp = PreferenceManager
                .getDefaultSharedPreferences(mPhone.getContext()).edit();
        sp.putBoolean(Phone.DATA_ROAMING_IS_USER_SETTING_KEY, true).commit();
        String key = Phone.DATA_ROAMING_IS_USER_SETTING_KEY + mPhone.getSubId();
        log("setDataRoamingFromUserAction: key=" + key);
        final SharedPreferences.Editor sp =
                PreferenceManager.getDefaultSharedPreferences(mPhone.getContext()).edit();
        sp.putBoolean(key, true).commit();
    }

    private @NonNull DataEnabledOverride getDataEnabledOverride() {
+92 −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.internal.telephony.data;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;

import android.os.Looper;
import android.os.PersistableBundle;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;

import com.android.internal.telephony.TelephonyTest;
import com.android.internal.telephony.data.DataSettingsManager.DataSettingsManagerCallback;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;

@RunWith(AndroidTestingRunner.class)
@TestableLooper.RunWithLooper
public class DataSettingsManagerTest extends TelephonyTest {
    private static final String DATA_ROAMING_IS_USER_SETTING = "data_roaming_is_user_setting_key0";

    // Mocked
    DataSettingsManagerCallback mMockedDataSettingsManagerCallback;

    DataSettingsManager mDataSettingsManagerUT;
    PersistableBundle mBundle;

    @Before
    public void setUp() throws Exception {
        logd("DataSettingsManagerTest +Setup!");
        super.setUp(getClass().getSimpleName());
        mMockedDataSettingsManagerCallback = Mockito.mock(DataSettingsManagerCallback.class);
        mBundle = mContextFixture.getCarrierConfigBundle();
        doReturn(true).when(mDataConfigManager).isConfigCarrierSpecific();

        doReturn("").when(mSubscriptionController).getDataEnabledOverrideRules(anyInt());

        mDataSettingsManagerUT = new DataSettingsManager(mPhone, mDataNetworkController,
                Looper.myLooper(), mMockedDataSettingsManagerCallback);
        logd("DataSettingsManagerTest -Setup!");
    }

    @After
    public void tearDown() throws Exception {
        logd("tearDown");
        super.tearDown();
    }

    @Test
    public void testDefaultDataRoamingEnabled() {
        doReturn(true).when(mDataConfigManager).isDataRoamingEnabledByDefault();
        mDataSettingsManagerUT.setDefaultDataRoamingEnabled();
        assertTrue(mDataSettingsManagerUT.isDataRoamingEnabled());

        mDataSettingsManagerUT.setDataRoamingEnabled(false);
        processAllMessages();
        assertFalse(mDataSettingsManagerUT.isDataRoamingEnabled());

        mDataSettingsManagerUT.setDefaultDataRoamingEnabled();
        assertFalse(mDataSettingsManagerUT.isDataRoamingEnabled());
    }

    @Test
    public void testDefaultDataRoamingEnabledFromUpgrade() {
        doReturn(true).when(mDataConfigManager).isDataRoamingEnabledByDefault();
        mContext.getSharedPreferences("", 0).edit()
                .putBoolean(DATA_ROAMING_IS_USER_SETTING, true).commit();
        mDataSettingsManagerUT.setDefaultDataRoamingEnabled();
        assertFalse(mDataSettingsManagerUT.isDataRoamingEnabled());
    }
}