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

Commit 3df93be0 authored by Jack Yu's avatar Jack Yu
Browse files

Split applyNewState into enable/disable APN

This is the preliminary work for adding handover
type for enable/disable APN.

Test: Telephony sanity tests + unit tests
Bug: 73659459

Merged-In: I5d5ece57555dc2227a306948aa9c79916f79146a
Change-Id: I5d5ece57555dc2227a306948aa9c79916f79146a
(cherry picked from commit fb9f69a6)
parent dc165514
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -419,7 +419,7 @@ public class ApnContext {
            } else {
                mLocalLogs.add(log);
                mNetworkRequests.add(networkRequest);
                mDcTracker.setEnabled(ApnSetting.getApnTypesBitmaskFromString(mApnType), true);
                mDcTracker.enableApn(ApnSetting.getApnTypesBitmaskFromString(mApnType));
            }
        }
    }
@@ -439,7 +439,7 @@ public class ApnContext {
                log.log("ApnContext.releaseNetwork left with " + mNetworkRequests.size() +
                        " requests.");
                if (mNetworkRequests.size() == 0) {
                    mDcTracker.setEnabled(ApnSetting.getApnTypesBitmaskFromString(mApnType), false);
                    mDcTracker.disableApn(ApnSetting.getApnTypesBitmaskFromString(mApnType));
                }
            }
        }
+114 −91
Original line number Diff line number Diff line
@@ -2116,83 +2116,6 @@ public class DcTracker extends Handler {
        setDataProfilesAsNeeded();
    }

    private void applyNewState(ApnContext apnContext, boolean enabled) {
        boolean cleanup = false;
        boolean trySetup = false;
        boolean met = apnContext.isDependencyMet();
        String str = "applyNewState(" + apnContext.getApnType() + ", " + enabled
                + "(" + apnContext.isEnabled() + "), " + met + "("
                + apnContext.isDependencyMet() + "))";
        if (DBG) log(str);
        apnContext.requestLog(str);

        if (apnContext.isReady()) {
            cleanup = true;
            if (enabled && met) {
                DctConstants.State state = apnContext.getState();
                switch(state) {
                    case CONNECTING:
                    case CONNECTED:
                    case DISCONNECTING:
                        // We're "READY" and active so just return
                        if (DBG) log("applyNewState: 'ready' so return");
                        apnContext.requestLog("applyNewState state=" + state + ", so return");
                        return;
                    case IDLE:
                        // fall through: this is unexpected but if it happens cleanup and try setup
                    case FAILED:
                    case RETRYING:
                        // We're "READY" but not active so disconnect (cleanup = true) and
                        // connect (trySetup = true) to be sure we retry the connection.
                        trySetup = true;
                        apnContext.setReason(Phone.REASON_DATA_ENABLED);
                        break;
                }
            } else if (met) {
                apnContext.setReason(Phone.REASON_DATA_DISABLED_INTERNAL);
                // If ConnectivityService has disabled this network, stop trying to bring
                // it up, but do not tear it down - ConnectivityService will do that
                // directly by talking with the DataConnection.
                //
                // This doesn't apply to DUN, however.  Those connections have special
                // requirements from carriers and we need stop using them when the dun
                // request goes away.  This applies to both CDMA and GSM because they both
                // can declare the DUN APN sharable by default traffic, thus still satisfying
                // those requests and not torn down organically.
                if ((apnContext.getApnType() == PhoneConstants.APN_TYPE_DUN && teardownForDun())
                        || apnContext.getState() != DctConstants.State.CONNECTED) {
                    str = "Clean up the connection. Apn type = " + apnContext.getApnType()
                            + ", state = " + apnContext.getState();
                    if (DBG) log(str);
                    apnContext.requestLog(str);
                    cleanup = true;
                } else {
                    cleanup = false;
                }
            } else {
                apnContext.setReason(Phone.REASON_DATA_DEPENDENCY_UNMET);
            }
        } else {
            if (enabled && met) {
                if (apnContext.isEnabled()) {
                    apnContext.setReason(Phone.REASON_DATA_DEPENDENCY_MET);
                } else {
                    apnContext.setReason(Phone.REASON_DATA_ENABLED);
                }
                if (apnContext.getState() == DctConstants.State.FAILED) {
                    apnContext.setState(DctConstants.State.IDLE);
                }
                trySetup = true;
            }
        }
        apnContext.setEnabled(enabled);
        if (cleanup) cleanUpConnectionInternal(true, apnContext);
        if (trySetup) {
            apnContext.resetErrorCodeRetries();
            trySetupData(apnContext);
        }
    }

    private DataConnection checkForCompatibleConnectedApnContext(ApnContext apnContext) {
        int apnType = apnContext.getApnTypeBitmask();
        ArrayList<ApnSetting> dunSettings = null;
@@ -2268,28 +2191,125 @@ public class DcTracker extends Handler {
        return null;
    }

    public void setEnabled(int apnType, boolean enable) {
        Message msg = obtainMessage(DctConstants.EVENT_ENABLE_NEW_APN);
    public void enableApn(int apnType) {
        Message msg = obtainMessage(DctConstants.EVENT_ENABLE_APN);
        msg.arg1 = apnType;
        sendMessage(msg);
    }

    private void onEnableApn(int apnType) {
        ApnContext apnContext = mApnContextsByType.get(apnType);
        if (apnContext == null) {
            loge("onEnableApn(" + apnType + "): NO ApnContext");
            return;
        }

        boolean trySetup = false;
        String str = "onEnableApn: apnType=" + ApnSetting.getApnTypeString(apnType);
        if (DBG) log(str);
        apnContext.requestLog(str);

        if (!apnContext.isDependencyMet()) {
            apnContext.setReason(Phone.REASON_DATA_DEPENDENCY_UNMET);
            apnContext.setEnabled(true);
            str = "onEnableApn: dependency is not met.";
            if (DBG) log(str);
            apnContext.requestLog(str);
            return;
        }

        if (apnContext.isReady()) {
            DctConstants.State state = apnContext.getState();
            switch(state) {
                case CONNECTING:
                case CONNECTED:
                case DISCONNECTING:
                    // We're "READY" and active so just return
                    if (DBG) log("onEnableApn: 'ready' so return");
                    apnContext.requestLog("onEnableApn state=" + state + ", so return");
                    return;
                case IDLE:
                    // fall through: this is unexpected but if it happens cleanup and try setup
                case FAILED:
                case RETRYING:
                    // We're "READY" but not active so disconnect (cleanup = true) and
                    // connect (trySetup = true) to be sure we retry the connection.
                    trySetup = true;
                    apnContext.setReason(Phone.REASON_DATA_ENABLED);
                    break;
            }
        } else {
            if (apnContext.isEnabled()) {
                apnContext.setReason(Phone.REASON_DATA_DEPENDENCY_MET);
            } else {
                apnContext.setReason(Phone.REASON_DATA_ENABLED);
            }
            if (apnContext.getState() == DctConstants.State.FAILED) {
                apnContext.setState(DctConstants.State.IDLE);
            }
            trySetup = true;
        }
        apnContext.setEnabled(true);
        if (trySetup) {
            apnContext.resetErrorCodeRetries();
            trySetupData(apnContext);
        }
    }

    public void disableApn(int apnType) {
        Message msg = obtainMessage(DctConstants.EVENT_DISABLE_APN);
        msg.arg1 = apnType;
        msg.arg2 = (enable ? DctConstants.ENABLED : DctConstants.DISABLED);
        sendMessage(msg);
    }

    private void onEnableApn(int apnType, int enabled) {
    private void onDisableApn(int apnType) {
        ApnContext apnContext = mApnContextsByType.get(apnType);
        if (apnContext == null) {
            loge("onEnableApn(" + apnType + ", " + enabled + "): NO ApnContext");
            loge("disableApn(" + apnType + "): NO ApnContext");
            return;
        }
        // TODO change our retry manager to use the appropriate numbers for the new APN
        if (DBG) log("onEnableApn: apnContext=" + apnContext + " call applyNewState");
        applyNewState(apnContext, enabled == DctConstants.ENABLED);

        if ((enabled == DctConstants.DISABLED) &&
            isOnlySingleDcAllowed(mPhone.getServiceState().getRilDataRadioTechnology()) &&
            !isHigherPriorityApnContextActive(apnContext)) {
        boolean cleanup = false;
        String str = "onDisableApn: apnType=" + ApnSetting.getApnTypeString(apnType);
        if (DBG) log(str);
        apnContext.requestLog(str);

            if(DBG) log("onEnableApn: isOnlySingleDcAllowed true & higher priority APN disabled");
        if (apnContext.isReady()) {
            cleanup = true;
            if (apnContext.isDependencyMet()) {
                apnContext.setReason(Phone.REASON_DATA_DISABLED_INTERNAL);
                // If ConnectivityService has disabled this network, stop trying to bring
                // it up, but do not tear it down - ConnectivityService will do that
                // directly by talking with the DataConnection.
                //
                // This doesn't apply to DUN, however.  Those connections have special
                // requirements from carriers and we need stop using them when the dun
                // request goes away.  This applies to both CDMA and GSM because they both
                // can declare the DUN APN sharable by default traffic, thus still satisfying
                // those requests and not torn down organically.
                if ((PhoneConstants.APN_TYPE_DUN.equals(apnContext.getApnType())
                        && teardownForDun())
                        || apnContext.getState() != DctConstants.State.CONNECTED) {
                    str = "Clean up the connection. Apn type = " + apnContext.getApnType()
                            + ", state = " + apnContext.getState();
                    if (DBG) log(str);
                    apnContext.requestLog(str);
                } else {
                    cleanup = false;
                }
            } else {
                apnContext.setReason(Phone.REASON_DATA_DEPENDENCY_UNMET);
            }
        }

        apnContext.setEnabled(false);
        if (cleanup) {
            cleanUpConnectionInternal(true, apnContext);
        }

        if (isOnlySingleDcAllowed(mPhone.getServiceState().getRilDataRadioTechnology())
                && !isHigherPriorityApnContextActive(apnContext)) {
            if (DBG) log("disableApn:isOnlySingleDcAllowed true & higher priority APN disabled");
            // If the highest priority APN is disabled and only single
            // data call is allowed, try to setup data call on other connectable APN.
            setupDataOnConnectableApns(Phone.REASON_SINGLE_PDN_ARBITRATION, RetryFailures.ALWAYS);
@@ -3393,8 +3413,11 @@ public class DcTracker extends Handler {
                    mProvisioningSpinner = null;
                }
                break;
            case DctConstants.EVENT_ENABLE_NEW_APN:
                onEnableApn(msg.arg1, msg.arg2);
            case DctConstants.EVENT_ENABLE_APN:
                onEnableApn(msg.arg1);
                break;
            case DctConstants.EVENT_DISABLE_APN:
                onDisableApn(msg.arg1);
                break;

            case DctConstants.EVENT_DATA_STALL_ALARM:
+4 −4
Original line number Diff line number Diff line
@@ -119,14 +119,14 @@ public class ApnContextTest extends TelephonyTest {
        NetworkRequest nr = new NetworkRequest.Builder().build();
        mApnContext.requestNetwork(nr, log);

        verify(mDcTracker, times(1)).setEnabled(eq(ApnSetting.TYPE_DEFAULT), eq(true));
        verify(mDcTracker, times(1)).enableApn(eq(ApnSetting.TYPE_DEFAULT));
        mApnContext.requestNetwork(nr, log);
        verify(mDcTracker, times(1)).setEnabled(eq(ApnSetting.TYPE_DEFAULT), eq(true));
        verify(mDcTracker, times(1)).enableApn(eq(ApnSetting.TYPE_DEFAULT));

        mApnContext.releaseNetwork(nr, log);
        verify(mDcTracker, times(1)).setEnabled(eq(ApnSetting.TYPE_DEFAULT), eq(false));
        verify(mDcTracker, times(1)).disableApn(eq(ApnSetting.TYPE_DEFAULT));
        mApnContext.releaseNetwork(nr, log);
        verify(mDcTracker, times(1)).setEnabled(eq(ApnSetting.TYPE_DEFAULT), eq(false));
        verify(mDcTracker, times(1)).disableApn(eq(ApnSetting.TYPE_DEFAULT));
    }

    @Test
+14 −14
Original line number Diff line number Diff line
@@ -587,9 +587,9 @@ public class DcTrackerTest extends TelephonyTest {
        mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
        waitForMs(200);

        logd("Sending EVENT_ENABLE_NEW_APN");
        logd("Sending EVENT_ENABLE_APN");
        // APN id 0 is APN_TYPE_DEFAULT
        mDct.setEnabled(ApnSetting.TYPE_DEFAULT, true);
        mDct.enableApn(ApnSetting.TYPE_DEFAULT);
        waitForMs(200);

        dataConnectionReasons = new DataConnectionReasons();
@@ -636,9 +636,9 @@ public class DcTrackerTest extends TelephonyTest {
        mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_DATA_CONNECTION_ATTACHED, null));
        waitForMs(200);

        logd("Sending EVENT_ENABLE_NEW_APN");
        logd("Sending EVENT_ENABLE_APN");
        // APN id 0 is APN_TYPE_DEFAULT
        mDct.setEnabled(ApnSetting.TYPE_DEFAULT, true);
        mDct.enableApn(ApnSetting.TYPE_DEFAULT);
        waitForMs(200);

        dataConnectionReasons = new DataConnectionReasons();
@@ -692,8 +692,8 @@ public class DcTrackerTest extends TelephonyTest {
        //set Default and MMS to be metered in the CarrierConfigManager
        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
        mDct.setEnabled(ApnSetting.TYPE_IMS, true);
        mDct.setEnabled(ApnSetting.TYPE_DEFAULT, true);
        mDct.enableApn(ApnSetting.TYPE_IMS);
        mDct.enableApn(ApnSetting.TYPE_DEFAULT);

        logd("Sending EVENT_RECORDS_LOADED");
        mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
@@ -741,8 +741,8 @@ public class DcTrackerTest extends TelephonyTest {
        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});

        mDct.setEnabled(ApnSetting.TYPE_IMS, true);
        mDct.setEnabled(ApnSetting.TYPE_DEFAULT, true);
        mDct.enableApn(ApnSetting.TYPE_IMS);
        mDct.enableApn(ApnSetting.TYPE_DEFAULT);

        logd("Sending EVENT_RECORDS_LOADED");
        mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
@@ -793,8 +793,8 @@ public class DcTrackerTest extends TelephonyTest {
        //set Default and MMS to be metered in the CarrierConfigManager
        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_ROAMING_APN_TYPES_STRINGS,
                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});
        mDct.setEnabled(ApnSetting.TYPE_IMS, true);
        mDct.setEnabled(ApnSetting.TYPE_DEFAULT, true);
        mDct.enableApn(ApnSetting.TYPE_IMS);
        mDct.enableApn(ApnSetting.TYPE_DEFAULT);

        logd("Sending DISABLE_ROAMING_CMD");
        mDct.setDataRoamingEnabledByUser(false);
@@ -905,8 +905,8 @@ public class DcTrackerTest extends TelephonyTest {
        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
                new String[]{PhoneConstants.APN_TYPE_DEFAULT, PhoneConstants.APN_TYPE_MMS});

        mDct.setEnabled(ApnSetting.TYPE_IMS, true);
        mDct.setEnabled(ApnSetting.TYPE_DEFAULT, true);
        mDct.enableApn(ApnSetting.TYPE_IMS);
        mDct.enableApn(ApnSetting.TYPE_DEFAULT);

        logd("Sending EVENT_RECORDS_LOADED");
        mDct.sendMessage(mDct.obtainMessage(DctConstants.EVENT_RECORDS_LOADED, null));
@@ -1194,7 +1194,7 @@ public class DcTrackerTest extends TelephonyTest {
                .getRilDataRadioTechnology();
        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
                new String[]{PhoneConstants.APN_TYPE_DEFAULT});
        mDct.setEnabled(ApnSetting.TYPE_DEFAULT, true);
        mDct.enableApn(ApnSetting.TYPE_DEFAULT);
        initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});

        logd("Sending EVENT_RECORDS_LOADED");
@@ -1314,7 +1314,7 @@ public class DcTrackerTest extends TelephonyTest {
                .getRilDataRadioTechnology();
        mBundle.putStringArray(CarrierConfigManager.KEY_CARRIER_METERED_APN_TYPES_STRINGS,
                new String[]{PhoneConstants.APN_TYPE_DEFAULT});
        mDct.setEnabled(ApnSetting.TYPE_DEFAULT, true);
        mDct.enableApn(ApnSetting.TYPE_DEFAULT);
        initApns(PhoneConstants.APN_TYPE_DEFAULT, new String[]{PhoneConstants.APN_TYPE_ALL});

        logd("Sending EVENT_RECORDS_LOADED");