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

Commit a27755fe authored by Ling Ma's avatar Ling Ma
Browse files

Adopt prev preferred APN on APN reset

To have the APN selected UI in sync with the currently connected network, while carrier config didn't specify any default APN, we adopt the previously preferred APN(the one used by the current connected network) if it's provided by carriers versus user created.

Fix: 237904428
Test: atest DataProfileManagerTest.java + manual
Change-Id: I27a9b62860a966ecfd84fc83ba70203136622ed0
Merged-In: I27a9b62860a966ecfd84fc83ba70203136622ed0
parent b5b1ea3a
Loading
Loading
Loading
Loading
+27 −6
Original line number Diff line number Diff line
@@ -487,6 +487,16 @@ public class DataProfileManager extends Handler {
                if (preferredDataProfile != null) {
                    // Save the carrier specified preferred data profile into database
                    setPreferredDataProfile(preferredDataProfile);
                } else {
                    preferredDataProfile = mAllDataProfiles.stream()
                            .filter(dp -> areDataProfileSharingApn(dp, mPreferredDataProfile))
                            .findFirst()
                            .orElse(null);
                    if (preferredDataProfile != null) {
                        log("updatePreferredDataProfile: preferredDB is empty and no carrier "
                                + "default configured, setting preferred to be prev preferred DP.");
                        setPreferredDataProfile(preferredDataProfile);
                    }
                }
            }
        } else {
@@ -702,13 +712,14 @@ public class DataProfileManager extends Handler {
    }

    /**
     * Check if the data profile is the preferred data profile.
     * Check if the data profile is essentially the preferred data profile. The non-essential
     * elements include e.g.APN Id.
     *
     * @param dataProfile The data profile to check.
     * @return {@code true} if the data profile is the preferred data profile.
     * @return {@code true} if the data profile is essentially the preferred data profile.
     */
    public boolean isDataProfilePreferred(@NonNull DataProfile dataProfile) {
        return dataProfile.equals(mPreferredDataProfile);
        return areDataProfileSharingApn(dataProfile, mPreferredDataProfile);
    }

    /**
@@ -935,9 +946,19 @@ public class DataProfileManager extends Handler {

        // Only check the APN from the profile is compatible or not.
        return mAllDataProfiles.stream()
                .filter(dp -> dp.getApnSetting() != null)
                .anyMatch(dp -> dp.getApnSetting().equals(dataProfile.getApnSetting(),
                        mPhone.getServiceState().getDataRoamingFromRegistration()));
                .anyMatch(dp -> areDataProfileSharingApn(dataProfile, dp));
    }

    /**
     * @return {@code true} if both data profiles' APN setting are non-null and essentially the same
     * (non-essential elements include e.g.APN Id).
     */
    private boolean areDataProfileSharingApn(@Nullable DataProfile a, @Nullable DataProfile b) {
        return a != null
                && b != null
                && a.getApnSetting() != null
                && a.getApnSetting().equals(b.getApnSetting(),
                mPhone.getServiceState().getDataRoamingFromRegistration());
    }

    /**
+42 −6
Original line number Diff line number Diff line
@@ -65,8 +65,10 @@ import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@RunWith(AndroidTestingRunner.class)
@@ -303,6 +305,8 @@ public class DataProfileManagerTest extends TelephonyTest {
                }
        );

        private Set<Object> mDeletedApns = new HashSet<>();

        @Override
        public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
                String sortOrder) {
@@ -324,9 +328,11 @@ public class DataProfileManagerTest extends TelephonyTest {
                    MatrixCursor mc = new MatrixCursor(APN_COLUMNS);
                    if (mSimInserted) {
                        for (Object apnSetting : mAllApnSettings) {
                            if (!mDeletedApns.contains(apnSetting)) {
                                mc.addRow((Object[]) apnSetting);
                            }
                        }
                    }
                    return mc;
                }
            } else if (isPathPrefixMatch(uri,
@@ -339,6 +345,7 @@ public class DataProfileManagerTest extends TelephonyTest {
                return mc;
            } else if (uri.isPathPrefixMatch(Telephony.Carriers.PREFERRED_APN_URI)) {
                for (Object apnSetting : mAllApnSettings) {
                    if (!mDeletedApns.contains(apnSetting)) {
                        int id = (int) ((Object[]) apnSetting)[0];
                        if (id == mPreferredApnId) {
                            MatrixCursor mc = new MatrixCursor(APN_COLUMNS);
@@ -347,6 +354,7 @@ public class DataProfileManagerTest extends TelephonyTest {
                        }
                    }
                }
            }

            return null;
        }
@@ -381,6 +389,21 @@ public class DataProfileManagerTest extends TelephonyTest {
            }
            return null;
        }

        public boolean removeApnByApnId(int apnId) {
            for (Object apnSetting : mAllApnSettings) {
                int id = (int) ((Object[]) apnSetting)[0];
                if (apnId == id) {
                    mDeletedApns.add(apnSetting);
                    return true;
                }
            }
            return false;
        }

        public void restoreApnSettings() {
            mDeletedApns.clear();
        }
    }

    /**
@@ -877,9 +900,22 @@ public class DataProfileManagerTest extends TelephonyTest {
        mDataProfileManagerUT.obtainMessage(2 /*EVENT_APN_DATABASE_CHANGED*/).sendToTarget();
        processAllMessages();

        // preferred APN should set to be the prev preferred
        assertThat(mDataProfileManagerUT.isAnyPreferredDataProfileExisting()).isTrue();
        assertThat(mDataProfileManagerUT.isDataProfilePreferred(dataProfile)).isTrue();

        //APN reset and removed GENERAL_PURPOSE_APN(as if user created) from APN DB
        mPreferredApnId = -1;
        mApnSettingContentProvider.removeApnByApnId(1);
        mDataProfileManagerUT.obtainMessage(2 /*EVENT_APN_DATABASE_CHANGED*/).sendToTarget();
        processAllMessages();

        // There should be no preferred APN after APN reset
        assertThat(mDataProfileManagerUT.isAnyPreferredDataProfileExisting()).isFalse();
        assertThat(mDataProfileManagerUT.isDataProfilePreferred(dataProfile)).isFalse();

        // restore mApnSettingContentProvider
        mApnSettingContentProvider.restoreApnSettings();
    }

    @Test