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

Commit bd35a1b6 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Re-evaluate detached unsatisfied network request"

parents f0b582a4 122c05b1
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -238,7 +238,9 @@ public class DataEvaluation {
        /** Query from {@link TelephonyManager#isDataConnectivityPossible()}. */
        EXTERNAL_QUERY(false),
        /** Tracking area code changed. */
        TAC_CHANGED(true);
        TAC_CHANGED(true),
        /** Unsatisfied network request detached. */
        UNSATISFIED_REQUEST_DETACHED(true);

        /**
         * {@code true} if the evaluation is due to environmental changes (i.e. SIM removal,
+34 −7
Original line number Diff line number Diff line
@@ -853,6 +853,14 @@ public class DataNetwork extends StateMachine {
         * @param dataNetwork The data network.
         */
        public abstract void onTrackNetworkUnwanted(@NonNull DataNetwork dataNetwork);

        /**
         * Called when a network request is detached after no longer satisfied.
         *
         * @param networkRequest The detached network request.
         */
        public abstract void onRetryUnsatisfiedNetworkRequest(
                @NonNull TelephonyNetworkRequest networkRequest);
    }

    /**
@@ -1114,7 +1122,8 @@ public class DataNetwork extends StateMachine {
                    break;
                }
                case EVENT_DETACH_NETWORK_REQUEST: {
                    onDetachNetworkRequest((TelephonyNetworkRequest) msg.obj);
                    onDetachNetworkRequest((TelephonyNetworkRequest) msg.obj,
                            msg.arg1 != 0 /* shouldRetry */);
                    updateNetworkScore();
                    break;
                }
@@ -1691,12 +1700,21 @@ public class DataNetwork extends StateMachine {
     * Called when detaching the network request from this data network.
     *
     * @param networkRequest Network request to detach.
     * @param shouldRetry {@code true} if the detached network request should be retried.
     */
    private void onDetachNetworkRequest(@NonNull TelephonyNetworkRequest networkRequest) {
    private void onDetachNetworkRequest(@NonNull TelephonyNetworkRequest networkRequest,
            boolean shouldRetry) {
        mAttachedNetworkRequestList.remove(networkRequest);
        networkRequest.setState(TelephonyNetworkRequest.REQUEST_STATE_UNSATISFIED);
        networkRequest.setAttachedNetwork(null);

        if (shouldRetry) {
            // Inform DataNetworkController that a network request was detached and should be
            // scheduled to retry.
            mDataNetworkCallback.invokeFromExecutor(
                    () -> mDataNetworkCallback.onRetryUnsatisfiedNetworkRequest(networkRequest));
        }

        if (mAttachedNetworkRequestList.isEmpty()) {
            log("All network requests are detached.");

@@ -1717,12 +1735,15 @@ public class DataNetwork extends StateMachine {
     * network.
     *
     * @param networkRequest Network request to detach.
     * @param shouldRetry {@code true} if the detached network request should be retried.
     */
    public void detachNetworkRequest(@NonNull TelephonyNetworkRequest networkRequest) {
    public void detachNetworkRequest(@NonNull TelephonyNetworkRequest networkRequest,
            boolean shouldRetry) {
        if (getCurrentState() == null || isDisconnected()) {
            return;
        }
        sendMessage(obtainMessage(EVENT_DETACH_NETWORK_REQUEST, networkRequest));
        sendMessage(obtainMessage(EVENT_DETACH_NETWORK_REQUEST, shouldRetry ? 1 : 0, 0,
                networkRequest));
    }

    /**
@@ -1773,13 +1794,15 @@ public class DataNetwork extends StateMachine {

    /**
     * Remove network requests that can't be satisfied anymore.
     *
     * @param shouldRetry {@code true} if the detached network requests should be retried.
     */
    private void removeUnsatisfiedNetworkRequests() {
    private void removeUnsatisfiedNetworkRequests(boolean shouldRetry) {
        for (TelephonyNetworkRequest networkRequest : mAttachedNetworkRequestList) {
            if (!networkRequest.canBeSatisfiedBy(mNetworkCapabilities)) {
                log("removeUnsatisfiedNetworkRequests: " + networkRequest
                        + " can't be satisfied anymore. Will be detached.");
                detachNetworkRequest(networkRequest);
                detachNetworkRequest(networkRequest, shouldRetry);
            }
        }
    }
@@ -2081,7 +2104,11 @@ public class DataNetwork extends StateMachine {
                mNetworkAgent.sendNetworkCapabilities(mNetworkCapabilities);
            }

            removeUnsatisfiedNetworkRequests();
            // Only retry the request when the network is in connected or handover state. This is to
            // prevent request is detached during connecting state, and then become a setup/detach
            // infinite loop.
            boolean shouldRetry = isConnected() || isHandoverInProgress();
            removeUnsatisfiedNetworkRequests(shouldRetry);
            mDataNetworkCallback.invokeFromExecutor(() -> mDataNetworkCallback
                    .onNetworkCapabilitiesChanged(DataNetwork.this));
        } else {
+30 −1
Original line number Diff line number Diff line
@@ -220,6 +220,13 @@ public class DataNetworkController extends Handler {
    private static final long REEVALUATE_UNSATISFIED_NETWORK_REQUESTS_TAC_CHANGED_DELAY_MILLIS =
            TimeUnit.MILLISECONDS.toMillis(100);

    /**
     * The delay in milliseconds to re-evaluate unsatisfied network requests after network request
     * detached.
     */
    private static final long REEVALUATE_UNSATISFIED_NETWORK_REQUESTS_AFTER_DETACHED_DELAY_MILLIS =
            TimeUnit.SECONDS.toMillis(1);

    private final Phone mPhone;
    private final String mLogTag;
    private final LocalLog mLocalLog = new LocalLog(128);
@@ -2136,7 +2143,8 @@ public class DataNetworkController extends Handler {
        }

        if (networkRequest.getAttachedNetwork() != null) {
            networkRequest.getAttachedNetwork().detachNetworkRequest(networkRequest);
            networkRequest.getAttachedNetwork().detachNetworkRequest(
                        networkRequest, false /* shouldRetry */);
        }
        log("onRemoveNetworkRequest: Removed " + networkRequest);
    }
@@ -2533,6 +2541,13 @@ public class DataNetworkController extends Handler {
                    public void onTrackNetworkUnwanted(@NonNull DataNetwork dataNetwork) {
                        DataNetworkController.this.onTrackNetworkUnwanted();
                    }

                    @Override
                    public void onRetryUnsatisfiedNetworkRequest(
                            @NonNull TelephonyNetworkRequest networkRequest) {
                        DataNetworkController.this.onRetryUnsatisfiedNetworkRequest(
                                networkRequest);
                    }
                }
        ));
        if (!mAnyDataNetworkExisting) {
@@ -2949,6 +2964,20 @@ public class DataNetworkController extends Handler {
        log("Failed to attach " + requestList + " to " + dataNetwork);
    }

    /**
     * Called when a network request is detached from the data network and should be retried.
     *
     * @param networkRequest The detached network request.
     */
    private void onRetryUnsatisfiedNetworkRequest(
            @NonNull TelephonyNetworkRequest networkRequest) {
        if (!mAllNetworkRequestList.contains(networkRequest)) return;

        sendMessageDelayed(obtainMessage(EVENT_REEVALUATE_UNSATISFIED_NETWORK_REQUESTS,
                        DataEvaluationReason.UNSATISFIED_REQUEST_DETACHED),
                REEVALUATE_UNSATISFIED_NETWORK_REQUESTS_AFTER_DETACHED_DELAY_MILLIS);
    }

    /**
     * Called when data stall occurs and needed to tear down / setup a new data network for
     * internet. This event is from {@link DataStallRecoveryManager}.
+74 −0
Original line number Diff line number Diff line
@@ -338,6 +338,12 @@ public class DataNetworkControllerTest extends TelephonyTest {
                            "ENTERPRISE", 1).getBytes()))
            .build();

    private final DataProfile mLowLatencyDataProfile = new DataProfile.Builder()
            .setTrafficDescriptor(new TrafficDescriptor(null,
                    new TrafficDescriptor.OsAppId(TrafficDescriptor.OsAppId.ANDROID_OS_ID,
                            "PRIORITIZE_LATENCY", 1).getBytes()))
            .build();

    /** Data call response map. The first key is the transport type, the second key is the cid. */
    private final Map<Integer, Map<Integer, DataCallResponse>> mDataCallResponses = new HashMap<>();

@@ -4000,4 +4006,72 @@ public class DataNetworkControllerTest extends TelephonyTest {

        verifyConnectedNetworkHasCapabilities(NetworkCapabilities.NET_CAPABILITY_MMS);
    }

    @Test
    public void testTrafficDescriptionChangedDataRetry() throws Exception {
        List<TrafficDescriptor> tdList = List.of(
                new TrafficDescriptor.Builder()
                        .setOsAppId(new OsAppId(OsAppId.ANDROID_OS_ID, "PRIORITIZE_LATENCY", 1)
                                .getBytes()).build(),
                new TrafficDescriptor.Builder()
                        .setOsAppId(new OsAppId(OsAppId.ANDROID_OS_ID, "ENTERPRISE", 1).getBytes())
                        .build()
                );
        setSuccessfulSetupDataResponse(mMockedWwanDataServiceManager,
                createDataCallResponse(1, DataCallResponse.LINK_STATUS_ACTIVE, tdList));
        doReturn(mEnterpriseDataProfile).when(mDataProfileManager)
                .getDataProfileForNetworkRequest(any(TelephonyNetworkRequest.class), anyInt(),
                        anyBoolean());
        mDataNetworkControllerUT.addNetworkRequest(new TelephonyNetworkRequest(
                new NetworkRequest.Builder()
                        .addCapability(NetworkCapabilities.NET_CAPABILITY_ENTERPRISE)
                        .removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
                        .build(), mPhone));
        processAllMessages();
        verifyConnectedNetworkHasCapabilities(NetworkCapabilities.NET_CAPABILITY_ENTERPRISE,
                NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_LATENCY);

        mDataNetworkControllerUT.addNetworkRequest(new TelephonyNetworkRequest(
                new NetworkRequest.Builder()
                        .addCapability(NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_LATENCY)
                        .removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
                        .build(), mPhone));
        processAllMessages();
        List<DataNetwork> dataNetworkList = getDataNetworks();
        assertThat(dataNetworkList).hasSize(1);

        // Now remove low latency TD from the data call response.
        logd("Now remove PRIORITIZE_LATENCY");
        tdList = List.of(new TrafficDescriptor.Builder()
                .setOsAppId(new OsAppId(OsAppId.ANDROID_OS_ID, "ENTERPRISE", 1).getBytes())
                .build());
        mDataCallResponses.get(AccessNetworkConstants.TRANSPORT_TYPE_WWAN).put(1,
                createDataCallResponse(1, DataCallResponse.LINK_STATUS_ACTIVE, tdList));
        mDataCallListChangedRegistrants.get(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
                .notifyRegistrants(new AsyncResult(AccessNetworkConstants.TRANSPORT_TYPE_WWAN,
                        List.of(createDataCallResponse(1, DataCallResponse.LINK_STATUS_ACTIVE,
                                tdList)), null));

        tdList = List.of(new TrafficDescriptor.Builder()
                .setOsAppId(new OsAppId(OsAppId.ANDROID_OS_ID, "PRIORITIZE_LATENCY", 1).getBytes())
                .build());
        setSuccessfulSetupDataResponse(mMockedWwanDataServiceManager,
                createDataCallResponse(2, DataCallResponse.LINK_STATUS_ACTIVE, tdList));
        doReturn(mLowLatencyDataProfile).when(mDataProfileManager)
                .getDataProfileForNetworkRequest(any(TelephonyNetworkRequest.class), anyInt(),
                        anyBoolean());
        processAllFutureMessages();

        dataNetworkList = getDataNetworks();
        assertThat(dataNetworkList).hasSize(2);

        assertThat(dataNetworkList.get(0).getNetworkCapabilities().hasCapability(
                NetworkCapabilities.NET_CAPABILITY_ENTERPRISE)).isTrue();
        assertThat(dataNetworkList.get(0).getNetworkCapabilities().hasCapability(
                NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_LATENCY)).isFalse();
        assertThat(dataNetworkList.get(1).getNetworkCapabilities().hasCapability(
                NetworkCapabilities.NET_CAPABILITY_ENTERPRISE)).isFalse();
        assertThat(dataNetworkList.get(1).getNetworkCapabilities().hasCapability(
                NetworkCapabilities.NET_CAPABILITY_PRIORITIZE_LATENCY)).isTrue();
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -1446,7 +1446,7 @@ public class DataNetworkTest extends TelephonyTest {
        assertThat(mDataNetworkUT.getApnTypeNetworkCapability())
                .isEqualTo(NetworkCapabilities.NET_CAPABILITY_SUPL);

        mDataNetworkUT.detachNetworkRequest(networkRequest);
        mDataNetworkUT.detachNetworkRequest(networkRequest, false);
        processAllMessages();

        assertThat(mDataNetworkUT.getApnTypeNetworkCapability())
@@ -1469,7 +1469,7 @@ public class DataNetworkTest extends TelephonyTest {
        // SUPL priority is 80
        assertThat(mDataNetworkUT.getPriority()).isEqualTo(80);

        mDataNetworkUT.detachNetworkRequest(networkRequest);
        mDataNetworkUT.detachNetworkRequest(networkRequest, false);
        processAllMessages();

        // Internet priority is 20