Merge "Fix hidden usage of networkCapabilitiesForType"
am: 73661b9a3d
Change-Id: I8d854f79b4da4ee57ce0d3f5c631a3641d56e435
This commit is contained in:
@@ -16,9 +16,12 @@
|
|||||||
|
|
||||||
package com.android.server.connectivity.tethering;
|
package com.android.server.connectivity.tethering;
|
||||||
|
|
||||||
|
import static android.net.ConnectivityManager.TYPE_BLUETOOTH;
|
||||||
|
import static android.net.ConnectivityManager.TYPE_ETHERNET;
|
||||||
|
import static android.net.ConnectivityManager.TYPE_MOBILE;
|
||||||
import static android.net.ConnectivityManager.TYPE_MOBILE_DUN;
|
import static android.net.ConnectivityManager.TYPE_MOBILE_DUN;
|
||||||
import static android.net.ConnectivityManager.TYPE_MOBILE_HIPRI;
|
import static android.net.ConnectivityManager.TYPE_MOBILE_HIPRI;
|
||||||
import static android.net.ConnectivityManager.TYPE_NONE;
|
import static android.net.ConnectivityManager.TYPE_WIFI;
|
||||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_DUN;
|
import static android.net.NetworkCapabilities.NET_CAPABILITY_DUN;
|
||||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN;
|
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN;
|
||||||
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
|
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
|
||||||
@@ -35,8 +38,10 @@ import android.net.util.PrefixUtils;
|
|||||||
import android.net.util.SharedLog;
|
import android.net.util.SharedLog;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.util.SparseIntArray;
|
||||||
|
|
||||||
import com.android.internal.annotations.VisibleForTesting;
|
import com.android.internal.annotations.VisibleForTesting;
|
||||||
|
import com.android.internal.util.Preconditions;
|
||||||
import com.android.internal.util.StateMachine;
|
import com.android.internal.util.StateMachine;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -77,11 +82,25 @@ public class UpstreamNetworkMonitor {
|
|||||||
public static final int EVENT_ON_LINKPROPERTIES = 2;
|
public static final int EVENT_ON_LINKPROPERTIES = 2;
|
||||||
public static final int EVENT_ON_LOST = 3;
|
public static final int EVENT_ON_LOST = 3;
|
||||||
public static final int NOTIFY_LOCAL_PREFIXES = 10;
|
public static final int NOTIFY_LOCAL_PREFIXES = 10;
|
||||||
|
// This value is used by deprecated preferredUpstreamIfaceTypes selection which is default
|
||||||
|
// disabled.
|
||||||
|
@VisibleForTesting
|
||||||
|
public static final int TYPE_NONE = -1;
|
||||||
|
|
||||||
private static final int CALLBACK_LISTEN_ALL = 1;
|
private static final int CALLBACK_LISTEN_ALL = 1;
|
||||||
private static final int CALLBACK_DEFAULT_INTERNET = 2;
|
private static final int CALLBACK_DEFAULT_INTERNET = 2;
|
||||||
private static final int CALLBACK_MOBILE_REQUEST = 3;
|
private static final int CALLBACK_MOBILE_REQUEST = 3;
|
||||||
|
|
||||||
|
private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray();
|
||||||
|
static {
|
||||||
|
sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR);
|
||||||
|
sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR);
|
||||||
|
sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR);
|
||||||
|
sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI);
|
||||||
|
sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH);
|
||||||
|
sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET);
|
||||||
|
}
|
||||||
|
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
private final SharedLog mLog;
|
private final SharedLog mLog;
|
||||||
private final StateMachine mTarget;
|
private final StateMachine mTarget;
|
||||||
@@ -202,7 +221,7 @@ public class UpstreamNetworkMonitor {
|
|||||||
final int legacyType = mDunRequired ? TYPE_MOBILE_DUN : TYPE_MOBILE_HIPRI;
|
final int legacyType = mDunRequired ? TYPE_MOBILE_DUN : TYPE_MOBILE_HIPRI;
|
||||||
|
|
||||||
final NetworkRequest mobileUpstreamRequest = new NetworkRequest.Builder()
|
final NetworkRequest mobileUpstreamRequest = new NetworkRequest.Builder()
|
||||||
.setCapabilities(ConnectivityManager.networkCapabilitiesForType(legacyType))
|
.setCapabilities(networkCapabilitiesForType(legacyType))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// The existing default network and DUN callbacks will be notified.
|
// The existing default network and DUN callbacks will be notified.
|
||||||
@@ -354,16 +373,6 @@ public class UpstreamNetworkMonitor {
|
|||||||
notifyTarget(EVENT_ON_LINKPROPERTIES, network);
|
notifyTarget(EVENT_ON_LINKPROPERTIES, network);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleSuspended(Network network) {
|
|
||||||
if (!network.equals(mTetheringUpstreamNetwork)) return;
|
|
||||||
mLog.log("SUSPENDED current upstream: " + network);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleResumed(Network network) {
|
|
||||||
if (!network.equals(mTetheringUpstreamNetwork)) return;
|
|
||||||
mLog.log("RESUMED current upstream: " + network);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleLost(Network network) {
|
private void handleLost(Network network) {
|
||||||
// There are few TODOs within ConnectivityService's rematching code
|
// There are few TODOs within ConnectivityService's rematching code
|
||||||
// pertaining to spurious onLost() notifications.
|
// pertaining to spurious onLost() notifications.
|
||||||
@@ -452,20 +461,6 @@ public class UpstreamNetworkMonitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onNetworkSuspended(Network network) {
|
|
||||||
if (mCallbackType == CALLBACK_LISTEN_ALL) {
|
|
||||||
handleSuspended(network);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onNetworkResumed(Network network) {
|
|
||||||
if (mCallbackType == CALLBACK_LISTEN_ALL) {
|
|
||||||
handleResumed(network);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLost(Network network) {
|
public void onLost(Network network) {
|
||||||
if (mCallbackType == CALLBACK_DEFAULT_INTERNET) {
|
if (mCallbackType == CALLBACK_DEFAULT_INTERNET) {
|
||||||
@@ -510,7 +505,7 @@ public class UpstreamNetworkMonitor {
|
|||||||
for (int type : preferredTypes) {
|
for (int type : preferredTypes) {
|
||||||
NetworkCapabilities nc;
|
NetworkCapabilities nc;
|
||||||
try {
|
try {
|
||||||
nc = ConnectivityManager.networkCapabilitiesForType(type);
|
nc = networkCapabilitiesForType(type);
|
||||||
} catch (IllegalArgumentException iae) {
|
} catch (IllegalArgumentException iae) {
|
||||||
Log.e(TAG, "No NetworkCapabilities mapping for legacy type: " + type);
|
Log.e(TAG, "No NetworkCapabilities mapping for legacy type: " + type);
|
||||||
continue;
|
continue;
|
||||||
@@ -572,4 +567,28 @@ public class UpstreamNetworkMonitor {
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given a legacy type (TYPE_WIFI, ...) returns the corresponding NetworkCapabilities instance.
|
||||||
|
* This function is used for deprecated legacy type and be disabled by default.
|
||||||
|
*/
|
||||||
|
@VisibleForTesting
|
||||||
|
public static NetworkCapabilities networkCapabilitiesForType(int type) {
|
||||||
|
final NetworkCapabilities nc = new NetworkCapabilities();
|
||||||
|
|
||||||
|
// Map from type to transports.
|
||||||
|
final int notFound = -1;
|
||||||
|
final int transport = sLegacyTypeToTransport.get(type, notFound);
|
||||||
|
Preconditions.checkArgument(transport != notFound, "unknown legacy type: " + type);
|
||||||
|
nc.addTransportType(transport);
|
||||||
|
|
||||||
|
if (type == TYPE_MOBILE_DUN) {
|
||||||
|
nc.addCapability(NetworkCapabilities.NET_CAPABILITY_DUN);
|
||||||
|
// DUN is restricted network, see NetworkCapabilities#FORCE_RESTRICTED_CAPABILITIES.
|
||||||
|
nc.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
|
||||||
|
} else {
|
||||||
|
nc.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
|
||||||
|
}
|
||||||
|
return nc;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,13 +18,14 @@ package com.android.server.connectivity.tethering;
|
|||||||
|
|
||||||
import static android.net.ConnectivityManager.TYPE_MOBILE_DUN;
|
import static android.net.ConnectivityManager.TYPE_MOBILE_DUN;
|
||||||
import static android.net.ConnectivityManager.TYPE_MOBILE_HIPRI;
|
import static android.net.ConnectivityManager.TYPE_MOBILE_HIPRI;
|
||||||
import static android.net.ConnectivityManager.TYPE_NONE;
|
|
||||||
import static android.net.ConnectivityManager.TYPE_WIFI;
|
import static android.net.ConnectivityManager.TYPE_WIFI;
|
||||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_DUN;
|
import static android.net.NetworkCapabilities.NET_CAPABILITY_DUN;
|
||||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
|
import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
|
||||||
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
|
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
|
||||||
import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
|
import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
|
||||||
|
|
||||||
|
import static com.android.server.connectivity.tethering.UpstreamNetworkMonitor.TYPE_NONE;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
@@ -538,13 +539,15 @@ public class UpstreamNetworkMonitorTest {
|
|||||||
mUNM.selectPreferredUpstreamType(preferredTypes));
|
mUNM.selectPreferredUpstreamType(preferredTypes));
|
||||||
verify(mEntitleMgr, times(1)).maybeRunProvisioning();
|
verify(mEntitleMgr, times(1)).maybeRunProvisioning();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertSatisfiesLegacyType(int legacyType, UpstreamNetworkState ns) {
|
private void assertSatisfiesLegacyType(int legacyType, UpstreamNetworkState ns) {
|
||||||
if (legacyType == TYPE_NONE) {
|
if (legacyType == TYPE_NONE) {
|
||||||
assertTrue(ns == null);
|
assertTrue(ns == null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final NetworkCapabilities nc = ConnectivityManager.networkCapabilitiesForType(legacyType);
|
final NetworkCapabilities nc =
|
||||||
|
UpstreamNetworkMonitor.networkCapabilitiesForType(legacyType);
|
||||||
assertTrue(nc.satisfiedByNetworkCapabilities(ns.networkCapabilities));
|
assertTrue(nc.satisfiedByNetworkCapabilities(ns.networkCapabilities));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user