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

Commit 8a5f7c9e authored by Cody Kesting's avatar Cody Kesting
Browse files

Define ConnectivityDiagnosticsCallback IBinder impl.

An IBinder implementation of the ConnectivityDiagnosticsCallback AIDL is
needed so that apps can receive notfications with their registered
callbacks for callbacks coming from ConnectivityService.

Bug: 143187964
Bug: 147848028
Test: compiles
Test: atest FrameworksNetTests
Change-Id: Ie62678f794d81e1edee68977ec684a911b5070b7
parent 91b9d55c
Loading
Loading
Loading
Loading
+48 −0
Original line number Original line Diff line number Diff line
@@ -21,6 +21,7 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.annotation.StringDef;
import android.annotation.StringDef;
import android.content.Context;
import android.content.Context;
import android.os.Binder;
import android.os.Parcel;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable;
import android.os.PersistableBundle;
import android.os.PersistableBundle;
@@ -544,6 +545,53 @@ public class ConnectivityDiagnosticsManager {
                };
                };
    }
    }


    /** @hide */
    @VisibleForTesting
    public static class ConnectivityDiagnosticsBinder
            extends IConnectivityDiagnosticsCallback.Stub {
        @NonNull private final ConnectivityDiagnosticsCallback mCb;
        @NonNull private final Executor mExecutor;

        /** @hide */
        @VisibleForTesting
        public ConnectivityDiagnosticsBinder(
                @NonNull ConnectivityDiagnosticsCallback cb, @NonNull Executor executor) {
            this.mCb = cb;
            this.mExecutor = executor;
        }

        /** @hide */
        @VisibleForTesting
        public void onConnectivityReport(@NonNull ConnectivityReport report) {
            Binder.withCleanCallingIdentity(() -> {
                mExecutor.execute(() -> {
                    mCb.onConnectivityReport(report);
                });
            });
        }

        /** @hide */
        @VisibleForTesting
        public void onDataStallSuspected(@NonNull DataStallReport report) {
            Binder.withCleanCallingIdentity(() -> {
                mExecutor.execute(() -> {
                    mCb.onDataStallSuspected(report);
                });
            });
        }

        /** @hide */
        @VisibleForTesting
        public void onNetworkConnectivityReported(
                @NonNull Network network, boolean hasConnectivity) {
            Binder.withCleanCallingIdentity(() -> {
                mExecutor.execute(() -> {
                    mCb.onNetworkConnectivityReported(network, hasConnectivity);
                });
            });
        }
    }

    /**
    /**
     * Abstract base class for Connectivity Diagnostics callbacks. Used for notifications about
     * Abstract base class for Connectivity Diagnostics callbacks. Used for notifications about
     * network connectivity events. Must be extended by applications wanting notifications.
     * network connectivity events. Must be extended by applications wanting notifications.
+52 −0
Original line number Original line Diff line number Diff line
@@ -16,6 +16,8 @@


package android.net;
package android.net;


import static android.net.ConnectivityDiagnosticsManager.ConnectivityDiagnosticsBinder;
import static android.net.ConnectivityDiagnosticsManager.ConnectivityDiagnosticsCallback;
import static android.net.ConnectivityDiagnosticsManager.ConnectivityReport;
import static android.net.ConnectivityDiagnosticsManager.ConnectivityReport;
import static android.net.ConnectivityDiagnosticsManager.DataStallReport;
import static android.net.ConnectivityDiagnosticsManager.DataStallReport;


@@ -25,12 +27,19 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;


import android.os.PersistableBundle;
import android.os.PersistableBundle;


import org.junit.Before;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.runners.JUnit4;
import org.mockito.Mock;

import java.util.concurrent.Executor;


@RunWith(JUnit4.class)
@RunWith(JUnit4.class)
public class ConnectivityDiagnosticsManagerTest {
public class ConnectivityDiagnosticsManagerTest {
@@ -41,6 +50,19 @@ public class ConnectivityDiagnosticsManagerTest {
    private static final String BUNDLE_KEY = "key";
    private static final String BUNDLE_KEY = "key";
    private static final String BUNDLE_VALUE = "value";
    private static final String BUNDLE_VALUE = "value";


    private static final Executor INLINE_EXECUTOR = x -> x.run();

    @Mock private ConnectivityDiagnosticsCallback mCb;

    private ConnectivityDiagnosticsBinder mBinder;

    @Before
    public void setUp() {
        mCb = mock(ConnectivityDiagnosticsCallback.class);

        mBinder = new ConnectivityDiagnosticsBinder(mCb, INLINE_EXECUTOR);
    }

    private ConnectivityReport createSampleConnectivityReport() {
    private ConnectivityReport createSampleConnectivityReport() {
        final LinkProperties linkProperties = new LinkProperties();
        final LinkProperties linkProperties = new LinkProperties();
        linkProperties.setInterfaceName(INTERFACE_NAME);
        linkProperties.setInterfaceName(INTERFACE_NAME);
@@ -193,4 +215,34 @@ public class ConnectivityDiagnosticsManagerTest {
    public void testDataStallReportParcelUnparcel() {
    public void testDataStallReportParcelUnparcel() {
        assertParcelSane(createSampleDataStallReport(), 4);
        assertParcelSane(createSampleDataStallReport(), 4);
    }
    }

    @Test
    public void testConnectivityDiagnosticsCallbackOnConnectivityReport() {
        mBinder.onConnectivityReport(createSampleConnectivityReport());

        // The callback will be invoked synchronously by inline executor. Immediately check the
        // latch without waiting.
        verify(mCb).onConnectivityReport(eq(createSampleConnectivityReport()));
    }

    @Test
    public void testConnectivityDiagnosticsCallbackOnDataStallSuspected() {
        mBinder.onDataStallSuspected(createSampleDataStallReport());

        // The callback will be invoked synchronously by inline executor. Immediately check the
        // latch without waiting.
        verify(mCb).onDataStallSuspected(eq(createSampleDataStallReport()));
    }

    @Test
    public void testConnectivityDiagnosticsCallbackOnNetworkConnectivityReported() {
        final Network n = new Network(NET_ID);
        final boolean connectivity = true;

        mBinder.onNetworkConnectivityReported(n, connectivity);

        // The callback will be invoked synchronously by inline executor. Immediately check the
        // latch without waiting.
        verify(mCb).onNetworkConnectivityReported(eq(n), eq(connectivity));
    }
}
}