Connect metered DHCP hint for Wi-Fi networks.

When DHCP lease includes vendor info indicating that remote Wi-Fi
network is metered, advise NetworkPolicy.  Users can still manually
change the metered flag in Settings.

Also remove any policies belonging to removed Wi-Fi networks, and
teach isNetworkMetered() about ethernet networks.

Bug: 6344821, 6369307, 6365872
Change-Id: I108606c6fddf2d02828fcab011f3a1501415f1bc
This commit is contained in:
Jeff Sharkey
2012-04-19 23:01:08 -07:00
parent f0c4c65848
commit 9f6e4ba50e
4 changed files with 149 additions and 5 deletions

View File

@ -68,9 +68,14 @@ public class WifiInfo implements Parcelable {
private int mLinkSpeed;
private InetAddress mIpAddress;
private String mMacAddress;
/**
* Flag indicating that AP has hinted that upstream connection is metered,
* and sensitive to heavy data transfers.
*/
private boolean mMeteredHint;
WifiInfo() {
mSSID = null;
mBSSID = null;
@ -96,6 +101,7 @@ public class WifiInfo implements Parcelable {
mLinkSpeed = source.mLinkSpeed;
mIpAddress = source.mIpAddress;
mMacAddress = source.mMacAddress;
mMeteredHint = source.mMeteredHint;
}
}
@ -168,6 +174,16 @@ public class WifiInfo implements Parcelable {
return mMacAddress;
}
/** {@hide} */
public void setMeteredHint(boolean meteredHint) {
mMeteredHint = meteredHint;
}
/** {@hide} */
public boolean getMeteredHint() {
return mMeteredHint;
}
void setNetworkId(int id) {
mNetworkId = id;
}
@ -248,6 +264,15 @@ public class WifiInfo implements Parcelable {
}
}
/** {@hide} */
public static String removeDoubleQuotes(String string) {
final int length = string.length();
if ((length > 1) && (string.charAt(0) == '"') && (string.charAt(length - 1) == '"')) {
return string.substring(1, length - 1);
}
return string;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
@ -260,7 +285,8 @@ public class WifiInfo implements Parcelable {
append(mSupplicantState == null ? none : mSupplicantState).
append(", RSSI: ").append(mRssi).
append(", Link speed: ").append(mLinkSpeed).
append(", Net ID: ").append(mNetworkId);
append(", Net ID: ").append(mNetworkId).
append(", Metered hint: ").append(mMeteredHint);
return sb.toString();
}
@ -284,6 +310,7 @@ public class WifiInfo implements Parcelable {
dest.writeString(getSSID());
dest.writeString(mBSSID);
dest.writeString(mMacAddress);
dest.writeInt(mMeteredHint ? 1 : 0);
mSupplicantState.writeToParcel(dest, flags);
}
@ -303,6 +330,7 @@ public class WifiInfo implements Parcelable {
info.setSSID(in.readString());
info.mBSSID = in.readString();
info.mMacAddress = in.readString();
info.mMeteredHint = in.readInt() != 0;
info.mSupplicantState = SupplicantState.CREATOR.createFromParcel(in);
return info;
}

View File

@ -1642,6 +1642,7 @@ public class WifiStateMachine extends StateMachine {
mWifiInfo.setNetworkId(WifiConfiguration.INVALID_NETWORK_ID);
mWifiInfo.setRssi(MIN_RSSI);
mWifiInfo.setLinkSpeed(-1);
mWifiInfo.setMeteredHint(false);
setNetworkDetailedState(DetailedState.DISCONNECTED);
mWifiConfigStore.updateStatus(mLastNetworkId, DetailedState.DISCONNECTED);
@ -1713,6 +1714,7 @@ public class WifiStateMachine extends StateMachine {
mWifiConfigStore.setIpConfiguration(mLastNetworkId, dhcpInfoInternal);
InetAddress addr = NetworkUtils.numericToInetAddress(dhcpInfoInternal.ipAddress);
mWifiInfo.setInetAddress(addr);
mWifiInfo.setMeteredHint(dhcpInfoInternal.hasMeteredHint());
if (getNetworkDetailedState() == DetailedState.CONNECTED) {
//DHCP renewal in connected state
LinkProperties linkProperties = dhcpInfoInternal.makeLinkProperties();
@ -1735,6 +1737,7 @@ public class WifiStateMachine extends StateMachine {
loge("IP configuration failed");
mWifiInfo.setInetAddress(null);
mWifiInfo.setMeteredHint(false);
/**
* If we've exceeded the maximum number of retries for DHCP
* to a given network, disable the network