Merge "Cherrypick: Create ScoredNetwork Badging API changes."

This commit is contained in:
Treehugger Robot
2017-01-09 23:39:47 +00:00
committed by Gerrit Code Review
3 changed files with 97 additions and 0 deletions

View File

@@ -25834,10 +25834,16 @@ package android.net {
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, android.os.Bundle);
method public int calculateBadge(int);
method public int describeContents();
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_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 final android.os.Bundle attributes;
field public final boolean meteredHint;
@@ -25845,6 +25851,9 @@ package android.net {
field public final android.net.RssiCurve rssiCurve;
}
public static abstract class ScoredNetwork.Badging implements java.lang.annotation.Annotation {
}
public class TrafficStats {
ctor public TrafficStats();
method public static void clearThreadStatsTag();

View File

@@ -16,6 +16,7 @@
package android.net;
import android.annotation.IntDef;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.os.Bundle;
@@ -24,6 +25,8 @@ import android.os.Parcelable;
import java.lang.Math;
import java.lang.UnsupportedOperationException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Objects;
/**
@@ -33,6 +36,15 @@ import java.util.Objects;
*/
@SystemApi
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
* 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 =
"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. */
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 =
new Parcelable.Creator<ScoredNetwork>() {
@Override

View File

@@ -166,4 +166,52 @@ public class ScoredNetworkTest {
assertTrue(newNetwork.meteredHint);
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);
}
}