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

Commit 6c929621 authored by Sundeep Ghuman's avatar Sundeep Ghuman
Browse files

Create ScoredNetwork Badging API changes.

Defines an attribute bundles key to hold the badging curve, the
enums for badge values, and an API to retrieve a badge for a
specific RSSI.

Bug: 33457699
Test: Unit tests
Change-Id: If9e8a60c4670b51c395fb501f55ddece8a72981e
parent 2c80a038
Loading
Loading
Loading
Loading
+9 −0
Original line number Original line Diff line number Diff line
@@ -26235,10 +26235,16 @@ package android.net {
    ctor public ScoredNetwork(android.net.NetworkKey, android.net.RssiCurve);
    ctor public ScoredNetwork(android.net.NetworkKey, android.net.RssiCurve);
    ctor public ScoredNetwork(android.net.NetworkKey, android.net.RssiCurve, boolean);
    ctor public ScoredNetwork(android.net.NetworkKey, android.net.RssiCurve, boolean);
    ctor public ScoredNetwork(android.net.NetworkKey, android.net.RssiCurve, boolean, android.os.Bundle);
    ctor public ScoredNetwork(android.net.NetworkKey, android.net.RssiCurve, boolean, android.os.Bundle);
    method public int calculateBadge(int);
    method public int describeContents();
    method public int describeContents();
    method public void writeToParcel(android.os.Parcel, int);
    method public void writeToParcel(android.os.Parcel, int);
    field public static final java.lang.String ATTRIBUTES_KEY_BADGING_CURVE = "android.net.attributes.key.BADGING_CURVE";
    field public static final java.lang.String ATTRIBUTES_KEY_HAS_CAPTIVE_PORTAL = "android.net.attributes.key.HAS_CAPTIVE_PORTAL";
    field public static final java.lang.String ATTRIBUTES_KEY_HAS_CAPTIVE_PORTAL = "android.net.attributes.key.HAS_CAPTIVE_PORTAL";
    field public static final java.lang.String ATTRIBUTES_KEY_RANKING_SCORE_OFFSET = "android.net.attributes.key.RANKING_SCORE_OFFSET";
    field public static final java.lang.String ATTRIBUTES_KEY_RANKING_SCORE_OFFSET = "android.net.attributes.key.RANKING_SCORE_OFFSET";
    field public static final int BADGING_4K = 30; // 0x1e
    field public static final int BADGING_HD = 20; // 0x14
    field public static final int BADGING_NONE = 0; // 0x0
    field public static final int BADGING_SD = 10; // 0xa
    field public static final android.os.Parcelable.Creator<android.net.ScoredNetwork> CREATOR;
    field public static final android.os.Parcelable.Creator<android.net.ScoredNetwork> CREATOR;
    field public final android.os.Bundle attributes;
    field public final android.os.Bundle attributes;
    field public final boolean meteredHint;
    field public final boolean meteredHint;
@@ -26246,6 +26252,9 @@ package android.net {
    field public final android.net.RssiCurve rssiCurve;
    field public final android.net.RssiCurve rssiCurve;
  }
  }
  public static abstract class ScoredNetwork.Badging implements java.lang.annotation.Annotation {
  }
  public class TrafficStats {
  public class TrafficStats {
    ctor public TrafficStats();
    ctor public TrafficStats();
    method public static void clearThreadStatsTag();
    method public static void clearThreadStatsTag();
+40 −0
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@


package android.net;
package android.net;


import android.annotation.IntDef;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.annotation.SystemApi;
import android.os.Bundle;
import android.os.Bundle;
@@ -24,6 +25,8 @@ import android.os.Parcelable;


import java.lang.Math;
import java.lang.Math;
import java.lang.UnsupportedOperationException;
import java.lang.UnsupportedOperationException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Objects;
import java.util.Objects;


/**
/**
@@ -33,6 +36,15 @@ import java.util.Objects;
 */
 */
@SystemApi
@SystemApi
public class ScoredNetwork implements Parcelable {
public class ScoredNetwork implements Parcelable {

  /**
     * Key used with the {@link #attributes} bundle to define the badging curve.
     *
     * <p>The badging curve is a {@link RssiCurve} used to map different RSSI values to {@link
     * Badging} enums.
     */
    public static final String ATTRIBUTES_KEY_BADGING_CURVE =
            "android.net.attributes.key.BADGING_CURVE";
    /**
    /**
     * Extra used with {@link #attributes} to specify whether the
     * Extra used with {@link #attributes} to specify whether the
     * network is believed to have a captive portal.
     * network is believed to have a captive portal.
@@ -58,6 +70,15 @@ public class ScoredNetwork implements Parcelable {
    public static final String ATTRIBUTES_KEY_RANKING_SCORE_OFFSET =
    public static final String ATTRIBUTES_KEY_RANKING_SCORE_OFFSET =
            "android.net.attributes.key.RANKING_SCORE_OFFSET";
            "android.net.attributes.key.RANKING_SCORE_OFFSET";


    @IntDef({BADGING_NONE, BADGING_SD, BADGING_HD, BADGING_4K})
    @Retention(RetentionPolicy.SOURCE)
    public @interface Badging {}

    public static final int BADGING_NONE = 0;
    public static final int BADGING_SD = 10;
    public static final int BADGING_HD = 20;
    public static final int BADGING_4K = 30;

    /** A {@link NetworkKey} uniquely identifying this network. */
    /** A {@link NetworkKey} uniquely identifying this network. */
    public final NetworkKey networkKey;
    public final NetworkKey networkKey;


@@ -249,6 +270,25 @@ public class ScoredNetwork implements Parcelable {
        }
        }
    }
    }


    /**
     * Return the {@link Badging} enum for this network for the given RSSI, derived from the
     * badging curve.
     *
     * <p>If no badging curve is present, {@link #BADGE_NONE} will be returned.
     *
     * @param rssi The rssi level for which the badge should be calculated
     */
    @Badging
    public int calculateBadge(int rssi) {
        if (attributes != null && attributes.containsKey(ATTRIBUTES_KEY_BADGING_CURVE)) {
            RssiCurve badgingCurve =
                    attributes.getParcelable(ATTRIBUTES_KEY_BADGING_CURVE);
            return badgingCurve.lookupScore(rssi);
        }

        return BADGING_NONE;
    }

    public static final Parcelable.Creator<ScoredNetwork> CREATOR =
    public static final Parcelable.Creator<ScoredNetwork> CREATOR =
            new Parcelable.Creator<ScoredNetwork>() {
            new Parcelable.Creator<ScoredNetwork>() {
                @Override
                @Override
+48 −0
Original line number Original line Diff line number Diff line
@@ -166,4 +166,52 @@ public class ScoredNetworkTest {
        assertTrue(newNetwork.meteredHint);
        assertTrue(newNetwork.meteredHint);
        assertNull(newNetwork.attributes);
        assertNull(newNetwork.attributes);
    }
    }

    @Test
    public void calculateBadgeShouldReturnNoBadgeWhenNoAttributesBundle() {
        ScoredNetwork network = new ScoredNetwork(KEY, CURVE);
        assertEquals(ScoredNetwork.BADGING_NONE, network.calculateBadge(TEST_RSSI));
    }

    @Test
    public void calculateBadgeShouldReturnNoBadgeWhenNoBadgingCurveInBundle() {
        ScoredNetwork network = new ScoredNetwork(KEY, CURVE, false /* meteredHint */, ATTRIBUTES);
        assertEquals(ScoredNetwork.BADGING_NONE, network.calculateBadge(TEST_RSSI));
    }

    @Test
    public void calculateBadgeShouldReturn4kBadge() {
        ScoredNetwork network =
            buildScoredNetworkWithGivenBadgeForTestRssi(ScoredNetwork.BADGING_4K);
        assertEquals(ScoredNetwork.BADGING_4K, network.calculateBadge(TEST_RSSI));
    }

    @Test
    public void calculateBadgeShouldReturnHdBadge() {
        ScoredNetwork network =
            buildScoredNetworkWithGivenBadgeForTestRssi(ScoredNetwork.BADGING_HD);
        assertEquals(ScoredNetwork.BADGING_HD, network.calculateBadge(TEST_RSSI));
    }

    @Test
    public void calculateBadgeShouldReturnSdBadge() {
        ScoredNetwork network =
            buildScoredNetworkWithGivenBadgeForTestRssi(ScoredNetwork.BADGING_SD);
        assertEquals(ScoredNetwork.BADGING_SD, network.calculateBadge(TEST_RSSI));
    }

    @Test
    public void calculateBadgeShouldReturnNoBadge() {
        ScoredNetwork network =
            buildScoredNetworkWithGivenBadgeForTestRssi(ScoredNetwork.BADGING_NONE);
        assertEquals(ScoredNetwork.BADGING_NONE, network.calculateBadge(TEST_RSSI));
    }

    private ScoredNetwork buildScoredNetworkWithGivenBadgeForTestRssi(int badge) {
        RssiCurve badgingCurve =
               new RssiCurve(RSSI_START, 10, new byte[] {0, 0, 0, 0, 0, 0, (byte) badge});
        Bundle attr = new Bundle();
        attr.putParcelable(ScoredNetwork.ATTRIBUTES_KEY_BADGING_CURVE, badgingCurve);
        return new ScoredNetwork(KEY, CURVE, false /* meteredHint */, attr);
    }
}
}