Support NetworkCallbacks based on signal strength.
Bug: 21405941 Change-Id: I2ed8a5aeb8dac464a4305671ed22abcacb485bc9
This commit is contained in:
parent
0b798a8331
commit
5d2656c181
3 changed files with 171 additions and 4 deletions
|
|
@ -184,6 +184,14 @@ public abstract class NetworkAgent extends Handler {
|
|||
*/
|
||||
public static final int EVENT_PACKET_KEEPALIVE = BASE + 13;
|
||||
|
||||
/**
|
||||
* Sent by ConnectivityService to inform this network transport of signal strength thresholds
|
||||
* that when crossed should trigger a system wakeup and a NetworkCapabilities update.
|
||||
*
|
||||
* obj = int[] describing signal strength thresholds.
|
||||
*/
|
||||
public static final int CMD_SET_SIGNAL_STRENGTH_THRESHOLDS = BASE + 14;
|
||||
|
||||
public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
|
||||
NetworkCapabilities nc, LinkProperties lp, int score) {
|
||||
this(looper, context, logTag, ni, nc, lp, score, null);
|
||||
|
|
@ -286,6 +294,11 @@ public abstract class NetworkAgent extends Handler {
|
|||
stopPacketKeepalive(msg);
|
||||
break;
|
||||
}
|
||||
|
||||
case CMD_SET_SIGNAL_STRENGTH_THRESHOLDS: {
|
||||
setSignalStrengthThresholds((int[]) msg.obj);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -445,6 +458,13 @@ public abstract class NetworkAgent extends Handler {
|
|||
queueOrSendMessage(EVENT_PACKET_KEEPALIVE, slot, reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by ConnectivityService to inform this network transport of signal strength thresholds
|
||||
* that when crossed should trigger a system wakeup and a NetworkCapabilities update.
|
||||
*/
|
||||
protected void setSignalStrengthThresholds(int[] thresholds) {
|
||||
}
|
||||
|
||||
protected void log(String s) {
|
||||
Log.d(LOG_TAG, "NetworkAgent: " + s);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,6 +48,7 @@ public final class NetworkCapabilities implements Parcelable {
|
|||
mLinkUpBandwidthKbps = nc.mLinkUpBandwidthKbps;
|
||||
mLinkDownBandwidthKbps = nc.mLinkDownBandwidthKbps;
|
||||
mNetworkSpecifier = nc.mNetworkSpecifier;
|
||||
mSignalStrength = nc.mSignalStrength;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -60,6 +61,7 @@ public final class NetworkCapabilities implements Parcelable {
|
|||
mNetworkCapabilities = mTransportTypes = 0;
|
||||
mLinkUpBandwidthKbps = mLinkDownBandwidthKbps = 0;
|
||||
mNetworkSpecifier = null;
|
||||
mSignalStrength = SIGNAL_STRENGTH_UNSPECIFIED;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -301,6 +303,7 @@ public final class NetworkCapabilities implements Parcelable {
|
|||
return "unknown non-requestable capabilities " + Long.toHexString(mNetworkCapabilities);
|
||||
}
|
||||
if (mLinkUpBandwidthKbps != 0 || mLinkDownBandwidthKbps != 0) return "link bandwidth";
|
||||
if (hasSignalStrength()) return "signalStrength";
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
@ -569,6 +572,68 @@ public final class NetworkCapabilities implements Parcelable {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic value that indicates no signal strength provided. A request specifying this value is
|
||||
* always satisfied.
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
public static final int SIGNAL_STRENGTH_UNSPECIFIED = Integer.MIN_VALUE;
|
||||
|
||||
/**
|
||||
* Signal strength. This is a signed integer, and higher values indicate better signal.
|
||||
* The exact units are bearer-dependent. For example, Wi-Fi uses RSSI.
|
||||
*/
|
||||
private int mSignalStrength;
|
||||
|
||||
/**
|
||||
* Sets the signal strength. This is a signed integer, with higher values indicating a stronger
|
||||
* signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same RSSI units
|
||||
* reported by WifiManager.
|
||||
* <p>
|
||||
* Note that when used to register a network callback, this specifies the minimum acceptable
|
||||
* signal strength. When received as the state of an existing network it specifies the current
|
||||
* value. A value of code SIGNAL_STRENGTH_UNSPECIFIED} means no value when received and has no
|
||||
* effect when requesting a callback.
|
||||
*
|
||||
* @param signalStrength the bearer-specific signal strength.
|
||||
* @hide
|
||||
*/
|
||||
public void setSignalStrength(int signalStrength) {
|
||||
mSignalStrength = signalStrength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if this object specifies a signal strength.
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
public boolean hasSignalStrength() {
|
||||
return mSignalStrength > SIGNAL_STRENGTH_UNSPECIFIED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the signal strength.
|
||||
*
|
||||
* @return The bearer-specific signal strength.
|
||||
* @hide
|
||||
*/
|
||||
public int getSignalStrength() {
|
||||
return mSignalStrength;
|
||||
}
|
||||
|
||||
private void combineSignalStrength(NetworkCapabilities nc) {
|
||||
this.mSignalStrength = Math.max(this.mSignalStrength, nc.mSignalStrength);
|
||||
}
|
||||
|
||||
private boolean satisfiedBySignalStrength(NetworkCapabilities nc) {
|
||||
return this.mSignalStrength <= nc.mSignalStrength;
|
||||
}
|
||||
|
||||
private boolean equalsSignalStrength(NetworkCapabilities nc) {
|
||||
return this.mSignalStrength == nc.mSignalStrength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Combine a set of Capabilities to this one. Useful for coming up with the complete set
|
||||
* @hide
|
||||
|
|
@ -578,6 +643,7 @@ public final class NetworkCapabilities implements Parcelable {
|
|||
combineTransportTypes(nc);
|
||||
combineLinkBandwidths(nc);
|
||||
combineSpecifiers(nc);
|
||||
combineSignalStrength(nc);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -594,7 +660,8 @@ public final class NetworkCapabilities implements Parcelable {
|
|||
satisfiedByNetCapabilities(nc, onlyImmutable) &&
|
||||
satisfiedByTransportTypes(nc) &&
|
||||
(onlyImmutable || satisfiedByLinkBandwidths(nc)) &&
|
||||
satisfiedBySpecifier(nc));
|
||||
satisfiedBySpecifier(nc) &&
|
||||
(onlyImmutable || satisfiedBySignalStrength(nc)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -639,6 +706,7 @@ public final class NetworkCapabilities implements Parcelable {
|
|||
return (equalsNetCapabilities(that) &&
|
||||
equalsTransportTypes(that) &&
|
||||
equalsLinkBandwidths(that) &&
|
||||
equalsSignalStrength(that) &&
|
||||
equalsSpecifier(that));
|
||||
}
|
||||
|
||||
|
|
@ -650,7 +718,8 @@ public final class NetworkCapabilities implements Parcelable {
|
|||
((int)(mTransportTypes >> 32) * 7) +
|
||||
(mLinkUpBandwidthKbps * 11) +
|
||||
(mLinkDownBandwidthKbps * 13) +
|
||||
(TextUtils.isEmpty(mNetworkSpecifier) ? 0 : mNetworkSpecifier.hashCode() * 17));
|
||||
(TextUtils.isEmpty(mNetworkSpecifier) ? 0 : mNetworkSpecifier.hashCode() * 17) +
|
||||
(mSignalStrength * 19));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -664,7 +733,9 @@ public final class NetworkCapabilities implements Parcelable {
|
|||
dest.writeInt(mLinkUpBandwidthKbps);
|
||||
dest.writeInt(mLinkDownBandwidthKbps);
|
||||
dest.writeString(mNetworkSpecifier);
|
||||
dest.writeInt(mSignalStrength);
|
||||
}
|
||||
|
||||
public static final Creator<NetworkCapabilities> CREATOR =
|
||||
new Creator<NetworkCapabilities>() {
|
||||
@Override
|
||||
|
|
@ -676,6 +747,7 @@ public final class NetworkCapabilities implements Parcelable {
|
|||
netCap.mLinkUpBandwidthKbps = in.readInt();
|
||||
netCap.mLinkDownBandwidthKbps = in.readInt();
|
||||
netCap.mNetworkSpecifier = in.readString();
|
||||
netCap.mSignalStrength = in.readInt();
|
||||
return netCap;
|
||||
}
|
||||
@Override
|
||||
|
|
@ -732,6 +804,8 @@ public final class NetworkCapabilities implements Parcelable {
|
|||
String specifier = (mNetworkSpecifier == null ?
|
||||
"" : " Specifier: <" + mNetworkSpecifier + ">");
|
||||
|
||||
return "[" + transports + capabilities + upBand + dnBand + specifier + "]";
|
||||
String signalStrength = (hasSignalStrength() ? " SignalStrength: " + mSignalStrength : "");
|
||||
|
||||
return "[" + transports + capabilities + upBand + dnBand + specifier + signalStrength + "]";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -185,6 +185,24 @@ public class NetworkRequest implements Parcelable {
|
|||
mNetworkCapabilities.setNetworkSpecifier(networkSpecifier);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the signal strength. This is a signed integer, with higher values indicating a
|
||||
* stronger signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same
|
||||
* RSSI units reported by WifiManager.
|
||||
* <p>
|
||||
* Note that when used to register a network callback, this specifies the minimum acceptable
|
||||
* signal strength. When received as the state of an existing network it specifies the
|
||||
* current value. A value of {@code SIGNAL_STRENGTH_UNSPECIFIED} means no value when
|
||||
* received and has no effect when requesting a callback.
|
||||
*
|
||||
* @param signalStrength the bearer-specific signal strength.
|
||||
* @hide
|
||||
*/
|
||||
public Builder setSignalStrength(int signalStrength) {
|
||||
mNetworkCapabilities.setSignalStrength(signalStrength);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
// implement the Parcelable interface
|
||||
|
|
|
|||
Loading…
Reference in a new issue