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

Commit 1e5ab9bb authored by Cody Kesting's avatar Cody Kesting
Browse files

Fire ConnectivityDiagnostics callbacks on Connectivity reported.

When ConnectivityService#reportNetworkTested is called, the platform
needs to fire ConnectivityDiagnostics callbacks for registered callbacks
that are permissioned for the network being reported on. This adds a new
event to ConnectivityDiagnosticsHandler for invoking these callbacks.

Bug: 143187964
Test: compiles
Test: atest CtsNetTestCases ConnectivityServiceTest
Change-Id: Icc6bcf7a2411133d8ecd7477bc351dad9333f24f
(cherry picked from commit 3d57b0f4ff5b56780c79df6062dfaf2b9fa5ae3c)
parent 4f49f140
Loading
Loading
Loading
Loading
+45 −0
Original line number Original line Diff line number Diff line
@@ -4205,6 +4205,19 @@ public class ConnectivityService extends IConnectivityManager.Stub
        final int connectivityInfo = encodeBool(hasConnectivity);
        final int connectivityInfo = encodeBool(hasConnectivity);
        mHandler.sendMessage(
        mHandler.sendMessage(
                mHandler.obtainMessage(EVENT_REVALIDATE_NETWORK, uid, connectivityInfo, network));
                mHandler.obtainMessage(EVENT_REVALIDATE_NETWORK, uid, connectivityInfo, network));

        final NetworkAgentInfo nai;
        if (network == null) {
            nai = getDefaultNetwork();
        } else {
            nai = getNetworkAgentInfoForNetwork(network);
        }
        if (nai != null) {
            mConnectivityDiagnosticsHandler.sendMessage(
                    mConnectivityDiagnosticsHandler.obtainMessage(
                            ConnectivityDiagnosticsHandler.EVENT_NETWORK_CONNECTIVITY_REPORTED,
                            connectivityInfo, 0, nai));
        }
    }
    }


    private void handleReportNetworkConnectivity(
    private void handleReportNetworkConnectivity(
@@ -7535,6 +7548,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
     */
     */
    @VisibleForTesting
    @VisibleForTesting
    class ConnectivityDiagnosticsHandler extends Handler {
    class ConnectivityDiagnosticsHandler extends Handler {
        private final String mTag = ConnectivityDiagnosticsHandler.class.getSimpleName();

        /**
        /**
         * Used to handle ConnectivityDiagnosticsCallback registration events from {@link
         * Used to handle ConnectivityDiagnosticsCallback registration events from {@link
         * android.net.ConnectivityDiagnosticsManager}.
         * android.net.ConnectivityDiagnosticsManager}.
@@ -7572,6 +7587,16 @@ public class ConnectivityService extends IConnectivityManager.Stub
         */
         */
        private static final int EVENT_DATA_STALL_SUSPECTED = 4;
        private static final int EVENT_DATA_STALL_SUSPECTED = 4;


        /**
         * Event for ConnectivityDiagnosticsHandler to handle network connectivity being reported to
         * the platform. This event will invoke {@link
         * IConnectivityDiagnosticsCallback#onNetworkConnectivityReported} for permissioned
         * callbacks.
         * obj = Network that was reported on
         * arg1 = boolint for the quality reported
         */
        private static final int EVENT_NETWORK_CONNECTIVITY_REPORTED = 5;

        private ConnectivityDiagnosticsHandler(Looper looper) {
        private ConnectivityDiagnosticsHandler(Looper looper) {
            super(looper);
            super(looper);
        }
        }
@@ -7613,6 +7638,13 @@ public class ConnectivityService extends IConnectivityManager.Stub
                    handleDataStallSuspected(nai, (long) msg.obj, msg.arg1, extras);
                    handleDataStallSuspected(nai, (long) msg.obj, msg.arg1, extras);
                    break;
                    break;
                }
                }
                case EVENT_NETWORK_CONNECTIVITY_REPORTED: {
                    handleNetworkConnectivityReported((NetworkAgentInfo) msg.obj, toBool(msg.arg1));
                    break;
                }
                default: {
                    Log.e(mTag, "Unrecognized event in ConnectivityDiagnostics: " + msg.what);
                }
            }
            }
        }
        }
    }
    }
@@ -7758,6 +7790,19 @@ public class ConnectivityService extends IConnectivityManager.Stub
        }
        }
    }
    }


    private void handleNetworkConnectivityReported(
            @NonNull NetworkAgentInfo nai, boolean connectivity) {
        final List<IConnectivityDiagnosticsCallback> results =
                getMatchingPermissionedCallbacks(nai);
        for (final IConnectivityDiagnosticsCallback cb : results) {
            try {
                cb.onNetworkConnectivityReported(nai.network, connectivity);
            } catch (RemoteException ex) {
                loge("Error invoking onNetworkConnectivityReported", ex);
            }
        }
    }

    private List<IConnectivityDiagnosticsCallback> getMatchingPermissionedCallbacks(
    private List<IConnectivityDiagnosticsCallback> getMatchingPermissionedCallbacks(
            @NonNull NetworkAgentInfo nai) {
            @NonNull NetworkAgentInfo nai) {
        final List<IConnectivityDiagnosticsCallback> results = new ArrayList<>();
        final List<IConnectivityDiagnosticsCallback> results = new ArrayList<>();
+27 −21
Original line number Original line Diff line number Diff line
@@ -6609,8 +6609,7 @@ public class ConnectivityServiceTest {
        mServiceContext.setPermission(perm, PERMISSION_GRANTED);
        mServiceContext.setPermission(perm, PERMISSION_GRANTED);
    }
    }


    @Test
    private void setUpConnectivityDiagnosticsCallback() throws Exception {
    public void testConnectivityDiagnosticsCallbackOnConnectivityReport() throws Exception {
        final NetworkRequest request = new NetworkRequest.Builder().build();
        final NetworkRequest request = new NetworkRequest.Builder().build();
        when(mConnectivityDiagnosticsCallback.asBinder()).thenReturn(mIBinder);
        when(mConnectivityDiagnosticsCallback.asBinder()).thenReturn(mIBinder);


@@ -6630,6 +6629,11 @@ public class ConnectivityServiceTest {
        mCellNetworkAgent.connect(true);
        mCellNetworkAgent.connect(true);
        callback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
        callback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
        callback.assertNoCallback();
        callback.assertNoCallback();
    }

    @Test
    public void testConnectivityDiagnosticsCallbackOnConnectivityReport() throws Exception {
        setUpConnectivityDiagnosticsCallback();


        // Wait for onConnectivityReport to fire
        // Wait for onConnectivityReport to fire
        verify(mConnectivityDiagnosticsCallback, timeout(TIMEOUT_MS))
        verify(mConnectivityDiagnosticsCallback, timeout(TIMEOUT_MS))
@@ -6638,25 +6642,7 @@ public class ConnectivityServiceTest {


    @Test
    @Test
    public void testConnectivityDiagnosticsCallbackOnDataStallSuspected() throws Exception {
    public void testConnectivityDiagnosticsCallbackOnDataStallSuspected() throws Exception {
        final NetworkRequest request = new NetworkRequest.Builder().build();
        setUpConnectivityDiagnosticsCallback();
        when(mConnectivityDiagnosticsCallback.asBinder()).thenReturn(mIBinder);

        mServiceContext.setPermission(
                android.Manifest.permission.NETWORK_STACK, PERMISSION_GRANTED);

        mService.registerConnectivityDiagnosticsCallback(
                mConnectivityDiagnosticsCallback, request, mContext.getPackageName());

        // Block until all other events are done processing.
        HandlerUtilsKt.waitForIdle(mCsHandlerThread, TIMEOUT_MS);

        // Connect the cell agent verify that it notifies TestNetworkCallback that it is available
        final TestNetworkCallback callback = new TestNetworkCallback();
        mCm.registerDefaultNetworkCallback(callback);
        mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
        mCellNetworkAgent.connect(true);
        callback.expectAvailableThenValidatedCallbacks(mCellNetworkAgent);
        callback.assertNoCallback();


        // Trigger notifyDataStallSuspected() on the INetworkMonitorCallbacks instance in the
        // Trigger notifyDataStallSuspected() on the INetworkMonitorCallbacks instance in the
        // cellular network agent
        // cellular network agent
@@ -6666,4 +6652,24 @@ public class ConnectivityServiceTest {
        verify(mConnectivityDiagnosticsCallback, timeout(TIMEOUT_MS))
        verify(mConnectivityDiagnosticsCallback, timeout(TIMEOUT_MS))
                .onDataStallSuspected(any(DataStallReport.class));
                .onDataStallSuspected(any(DataStallReport.class));
    }
    }

    @Test
    public void testConnectivityDiagnosticsCallbackOnConnectivityReported() throws Exception {
        setUpConnectivityDiagnosticsCallback();

        final Network n = mCellNetworkAgent.getNetwork();
        final boolean hasConnectivity = true;
        mService.reportNetworkConnectivity(n, hasConnectivity);

        // Wait for onNetworkConnectivityReported to fire
        verify(mConnectivityDiagnosticsCallback, timeout(TIMEOUT_MS))
                .onNetworkConnectivityReported(eq(n), eq(hasConnectivity));

        final boolean noConnectivity = false;
        mService.reportNetworkConnectivity(n, noConnectivity);

        // Wait for onNetworkConnectivityReported to fire
        verify(mConnectivityDiagnosticsCallback, timeout(TIMEOUT_MS))
                .onNetworkConnectivityReported(eq(n), eq(noConnectivity));
    }
}
}