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

Commit fc199ca7 authored by Cody Kesting's avatar Cody Kesting
Browse files

Make DataStallReport Parcelable.

DataStallReport is defined inside ConnectivityDiagnosticsManager. In
order for DataStallReport to be used in aidls, it must implement the
Parcelable interface.

Bug: 143187964
Test: compiles
Test: atest FrameworksNetTests
Change-Id: Idbb4885e2f67fb3f85d406a622ae45d34492dca4
parent 73328669
Loading
Loading
Loading
Loading
+10 −7
Original line number Diff line number Diff line
@@ -28683,8 +28683,6 @@ package android.net {
  public class ConnectivityDiagnosticsManager {
    method public void registerConnectivityDiagnosticsCallback(@NonNull android.net.NetworkRequest, @NonNull java.util.concurrent.Executor, @NonNull android.net.ConnectivityDiagnosticsManager.ConnectivityDiagnosticsCallback);
    method public void unregisterConnectivityDiagnosticsCallback(@NonNull android.net.ConnectivityDiagnosticsManager.ConnectivityDiagnosticsCallback);
    field public static final int DETECTION_METHOD_DNS_EVENTS = 1; // 0x1
    field public static final int DETECTION_METHOD_TCP_METRICS = 2; // 0x2
  }
  public abstract static class ConnectivityDiagnosticsManager.ConnectivityDiagnosticsCallback {
@@ -28706,12 +28704,17 @@ package android.net {
    field @NonNull public static final android.os.Parcelable.Creator<android.net.ConnectivityDiagnosticsManager.ConnectivityReport> CREATOR;
  }
  public static class ConnectivityDiagnosticsManager.DataStallReport {
  public static final class ConnectivityDiagnosticsManager.DataStallReport implements android.os.Parcelable {
    ctor public ConnectivityDiagnosticsManager.DataStallReport(@NonNull android.net.Network, long, int, @NonNull android.os.PersistableBundle);
    field public final int detectionMethod;
    field @NonNull public final android.net.Network network;
    field public final long reportTimestamp;
    field @NonNull public final android.os.PersistableBundle stallDetails;
    method public int describeContents();
    method public int getDetectionMethod();
    method @NonNull public android.net.Network getNetwork();
    method public long getReportTimestamp();
    method @NonNull public android.os.PersistableBundle getStallDetails();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.net.ConnectivityDiagnosticsManager.DataStallReport> CREATOR;
    field public static final int DETECTION_METHOD_DNS_EVENTS = 1; // 0x1
    field public static final int DETECTION_METHOD_TCP_METRICS = 2; // 0x2
  }
  public class ConnectivityManager {
+2 −1
Original line number Diff line number Diff line
@@ -18,3 +18,4 @@
package android.net;

parcelable ConnectivityDiagnosticsManager.ConnectivityReport;
parcelable ConnectivityDiagnosticsManager.DataStallReport;
 No newline at end of file
+109 −19
Original line number Diff line number Diff line
@@ -53,16 +53,6 @@ import java.util.concurrent.Executor;
 * </ul>
 */
public class ConnectivityDiagnosticsManager {
    public static final int DETECTION_METHOD_DNS_EVENTS = 1;
    public static final int DETECTION_METHOD_TCP_METRICS = 2;

    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef(
            prefix = {"DETECTION_METHOD_"},
            value = {DETECTION_METHOD_DNS_EVENTS, DETECTION_METHOD_TCP_METRICS})
    public @interface DetectionMethod {}

    /** @hide */
    public ConnectivityDiagnosticsManager() {}

@@ -237,21 +227,31 @@ public class ConnectivityDiagnosticsManager {
    }

    /** Class that includes information for a suspected data stall on a specific Network */
    public static class DataStallReport {
    public static final class DataStallReport implements Parcelable {
        public static final int DETECTION_METHOD_DNS_EVENTS = 1;
        public static final int DETECTION_METHOD_TCP_METRICS = 2;

        /** @hide */
        @Retention(RetentionPolicy.SOURCE)
        @IntDef(
                prefix = {"DETECTION_METHOD_"},
                value = {DETECTION_METHOD_DNS_EVENTS, DETECTION_METHOD_TCP_METRICS})
        public @interface DetectionMethod {}

        /** The Network for which this DataStallReport applied */
        @NonNull public final Network network;
        @NonNull private final Network mNetwork;

        /**
         * The timestamp for the report. The timestamp is taken from {@link
         * System#currentTimeMillis}.
         */
        public final long reportTimestamp;
        private long mReportTimestamp;

        /** The detection method used to identify the suspected data stall */
        @DetectionMethod public final int detectionMethod;
        @DetectionMethod private final int mDetectionMethod;

        /** PersistableBundle that may contain additional information on the suspected data stall */
        @NonNull public final PersistableBundle stallDetails;
        @NonNull private final PersistableBundle mStallDetails;

        /**
         * Constructor for DataStallReport.
@@ -270,11 +270,101 @@ public class ConnectivityDiagnosticsManager {
                long reportTimestamp,
                @DetectionMethod int detectionMethod,
                @NonNull PersistableBundle stallDetails) {
            this.network = network;
            this.reportTimestamp = reportTimestamp;
            this.detectionMethod = detectionMethod;
            this.stallDetails = stallDetails;
            mNetwork = network;
            mReportTimestamp = reportTimestamp;
            mDetectionMethod = detectionMethod;
            mStallDetails = stallDetails;
        }

        /**
         * Returns the Network for this DataStallReport.
         *
         * @return The Network for which this DataStallReport applied
         */
        @NonNull
        public Network getNetwork() {
            return mNetwork;
        }

        /**
         * Returns the epoch timestamp (milliseconds) for when this report was taken.
         *
         * @return The timestamp for the report. Taken from {@link System#currentTimeMillis}.
         */
        public long getReportTimestamp() {
            return mReportTimestamp;
        }

        /**
         * Returns the detection method used to identify this suspected data stall.
         *
         * @return The detection method used to identify the suspected data stall
         */
        public int getDetectionMethod() {
            return mDetectionMethod;
        }

        /**
         * Returns a PersistableBundle with additional info for this report.
         *
         * @return PersistableBundle that may contain additional information on the suspected data
         *     stall
         */
        @NonNull
        public PersistableBundle getStallDetails() {
            return new PersistableBundle(mStallDetails);
        }

        @Override
        public boolean equals(@Nullable Object o) {
            if (this == o) return true;
            if (!(o instanceof DataStallReport)) return false;
            final DataStallReport that = (DataStallReport) o;

            // PersistableBundle is optimized to avoid unparcelling data unless fields are
            // referenced. Because of this, use {@link ConnectivityDiagnosticsManager#equals} over
            // {@link PersistableBundle#kindofEquals}.
            return mReportTimestamp == that.mReportTimestamp
                    && mDetectionMethod == that.mDetectionMethod
                    && mNetwork.equals(that.mNetwork)
                    && persistableBundleEquals(mStallDetails, that.mStallDetails);
        }

        @Override
        public int hashCode() {
            return Objects.hash(mNetwork, mReportTimestamp, mDetectionMethod, mStallDetails);
        }

        /** {@inheritDoc} */
        @Override
        public int describeContents() {
            return 0;
        }

        /** {@inheritDoc} */
        @Override
        public void writeToParcel(@NonNull Parcel dest, int flags) {
            dest.writeParcelable(mNetwork, flags);
            dest.writeLong(mReportTimestamp);
            dest.writeInt(mDetectionMethod);
            dest.writeParcelable(mStallDetails, flags);
        }

        /** Implement the Parcelable interface */
        public static final @NonNull Creator<DataStallReport> CREATOR =
                new Creator<DataStallReport>() {
                    public DataStallReport createFromParcel(Parcel in) {
                        return new DataStallReport(
                                in.readParcelable(null),
                                in.readLong(),
                                in.readInt(),
                                in.readParcelable(null));
                    }

                    public DataStallReport[] newArray(int size) {
                        return new DataStallReport[size];
                    }
                };
    }

    /**
+38 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.net;

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

import static com.android.testutils.ParcelUtilsKt.assertParcelSane;

@@ -34,6 +35,7 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class ConnectivityDiagnosticsManagerTest {
    private static final int NET_ID = 1;
    private static final int DETECTION_METHOD = 2;
    private static final long TIMESTAMP = 10L;
    private static final String INTERFACE_NAME = "interface";
    private static final String BUNDLE_KEY = "key";
@@ -155,4 +157,40 @@ public class ConnectivityDiagnosticsManagerTest {
    public void testConnectivityReportParcelUnparcel() {
        assertParcelSane(createSampleConnectivityReport(), 5);
    }

    private DataStallReport createSampleDataStallReport() {
        final PersistableBundle bundle = new PersistableBundle();
        bundle.putString(BUNDLE_KEY, BUNDLE_VALUE);
        return new DataStallReport(new Network(NET_ID), TIMESTAMP, DETECTION_METHOD, bundle);
    }

    private DataStallReport createDefaultDataStallReport() {
        return new DataStallReport(new Network(0), 0L, 0, PersistableBundle.EMPTY);
    }

    @Test
    public void testDataStallReportEquals() {
        assertEquals(createSampleDataStallReport(), createSampleDataStallReport());
        assertEquals(createDefaultDataStallReport(), createDefaultDataStallReport());

        final PersistableBundle bundle = new PersistableBundle();
        bundle.putString(BUNDLE_KEY, BUNDLE_VALUE);

        assertNotEquals(
                createDefaultDataStallReport(),
                new DataStallReport(new Network(NET_ID), 0L, 0, PersistableBundle.EMPTY));
        assertNotEquals(
                createDefaultDataStallReport(),
                new DataStallReport(new Network(0), TIMESTAMP, 0, PersistableBundle.EMPTY));
        assertNotEquals(
                createDefaultDataStallReport(),
                new DataStallReport(new Network(0), 0L, DETECTION_METHOD, PersistableBundle.EMPTY));
        assertNotEquals(
                createDefaultDataStallReport(), new DataStallReport(new Network(0), 0L, 0, bundle));
    }

    @Test
    public void testDataStallReportParcelUnparcel() {
        assertParcelSane(createSampleDataStallReport(), 4);
    }
}