Merge "Minor improvement in logging in TetheringConfiguration"

am: 39ce589f0b

Change-Id: I1d61308cf21f3c4ebcc37f78bdd102f96a676462
This commit is contained in:
Erik Kline
2017-05-28 11:16:32 +00:00
committed by android-build-merger
3 changed files with 59 additions and 16 deletions

View File

@@ -235,7 +235,7 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
} }
private void updateConfiguration() { private void updateConfiguration() {
mConfig = new TetheringConfiguration(mContext); mConfig = new TetheringConfiguration(mContext, mLog);
} }
@Override @Override

View File

@@ -25,7 +25,7 @@ import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.util.Log; import android.net.util.SharedLog;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.ArrayList; import java.util.ArrayList;
@@ -74,7 +74,9 @@ public class TetheringConfiguration {
public final String[] dhcpRanges; public final String[] dhcpRanges;
public final String[] defaultIPv4DNS; public final String[] defaultIPv4DNS;
public TetheringConfiguration(Context ctx) { public TetheringConfiguration(Context ctx, SharedLog log) {
final SharedLog configLog = log.forSubComponent("config");
tetherableUsbRegexs = ctx.getResources().getStringArray( tetherableUsbRegexs = ctx.getResources().getStringArray(
com.android.internal.R.array.config_tether_usb_regexs); com.android.internal.R.array.config_tether_usb_regexs);
tetherableWifiRegexs = ctx.getResources().getStringArray( tetherableWifiRegexs = ctx.getResources().getStringArray(
@@ -83,11 +85,15 @@ public class TetheringConfiguration {
com.android.internal.R.array.config_tether_bluetooth_regexs); com.android.internal.R.array.config_tether_bluetooth_regexs);
final int dunCheck = checkDunRequired(ctx); final int dunCheck = checkDunRequired(ctx);
configLog.log("DUN check returned: " + dunCheckString(dunCheck));
preferredUpstreamIfaceTypes = getUpstreamIfaceTypes(ctx, dunCheck); preferredUpstreamIfaceTypes = getUpstreamIfaceTypes(ctx, dunCheck);
isDunRequired = preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN); isDunRequired = preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN);
dhcpRanges = getDhcpRanges(ctx); dhcpRanges = getDhcpRanges(ctx);
defaultIPv4DNS = copy(DEFAULT_IPV4_DNS); defaultIPv4DNS = copy(DEFAULT_IPV4_DNS);
configLog.log(toString());
} }
public boolean isUsb(String iface) { public boolean isUsb(String iface) {
@@ -110,21 +116,25 @@ public class TetheringConfiguration {
pw.print("isDunRequired: "); pw.print("isDunRequired: ");
pw.println(isDunRequired); pw.println(isDunRequired);
String[] upstreamTypes = null; dumpStringArray(pw, "preferredUpstreamIfaceTypes",
if (preferredUpstreamIfaceTypes != null) { preferredUpstreamNames(preferredUpstreamIfaceTypes));
upstreamTypes = new String[preferredUpstreamIfaceTypes.size()];
int i = 0;
for (Integer netType : preferredUpstreamIfaceTypes) {
upstreamTypes[i] = ConnectivityManager.getNetworkTypeName(netType);
i++;
}
}
dumpStringArray(pw, "preferredUpstreamIfaceTypes", upstreamTypes);
dumpStringArray(pw, "dhcpRanges", dhcpRanges); dumpStringArray(pw, "dhcpRanges", dhcpRanges);
dumpStringArray(pw, "defaultIPv4DNS", defaultIPv4DNS); dumpStringArray(pw, "defaultIPv4DNS", defaultIPv4DNS);
} }
public String toString() {
final StringJoiner sj = new StringJoiner(" ");
sj.add(String.format("tetherableUsbRegexs:%s", makeString(tetherableUsbRegexs)));
sj.add(String.format("tetherableWifiRegexs:%s", makeString(tetherableWifiRegexs)));
sj.add(String.format("tetherableBluetoothRegexs:%s",
makeString(tetherableBluetoothRegexs)));
sj.add(String.format("isDunRequired:%s", isDunRequired));
sj.add(String.format("preferredUpstreamIfaceTypes:%s",
makeString(preferredUpstreamNames(preferredUpstreamIfaceTypes))));
return String.format("TetheringConfiguration{%s}", sj.toString());
}
private static void dumpStringArray(PrintWriter pw, String label, String[] values) { private static void dumpStringArray(PrintWriter pw, String label, String[] values) {
pw.print(label); pw.print(label);
pw.print(": "); pw.print(": ");
@@ -140,11 +150,42 @@ public class TetheringConfiguration {
pw.println(); pw.println();
} }
private static String makeString(String[] strings) {
final StringJoiner sj = new StringJoiner(",", "[", "]");
for (String s : strings) sj.add(s);
return sj.toString();
}
private static String[] preferredUpstreamNames(Collection<Integer> upstreamTypes) {
String[] upstreamNames = null;
if (upstreamTypes != null) {
upstreamNames = new String[upstreamTypes.size()];
int i = 0;
for (Integer netType : upstreamTypes) {
upstreamNames[i] = ConnectivityManager.getNetworkTypeName(netType);
i++;
}
}
return upstreamNames;
}
private static int checkDunRequired(Context ctx) { private static int checkDunRequired(Context ctx) {
final TelephonyManager tm = (TelephonyManager) ctx.getSystemService(TELEPHONY_SERVICE); final TelephonyManager tm = (TelephonyManager) ctx.getSystemService(TELEPHONY_SERVICE);
return (tm != null) ? tm.getTetherApnRequired() : DUN_UNSPECIFIED; return (tm != null) ? tm.getTetherApnRequired() : DUN_UNSPECIFIED;
} }
private static String dunCheckString(int dunCheck) {
switch (dunCheck) {
case DUN_NOT_REQUIRED: return "DUN_NOT_REQUIRED";
case DUN_REQUIRED: return "DUN_REQUIRED";
case DUN_UNSPECIFIED: return "DUN_UNSPECIFIED";
default:
return String.format("UNKNOWN (%s)", dunCheck);
}
}
private static Collection<Integer> getUpstreamIfaceTypes(Context ctx, int dunCheck) { private static Collection<Integer> getUpstreamIfaceTypes(Context ctx, int dunCheck) {
final int ifaceTypes[] = ctx.getResources().getIntArray( final int ifaceTypes[] = ctx.getResources().getIntArray(
com.android.internal.R.array.config_tether_upstream_types); com.android.internal.R.array.config_tether_upstream_types);

View File

@@ -31,6 +31,7 @@ import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
import android.content.ContextWrapper; import android.content.ContextWrapper;
import android.content.res.Resources; import android.content.res.Resources;
import android.net.util.SharedLog;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4; import android.support.test.runner.AndroidJUnit4;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
@@ -47,6 +48,7 @@ import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
@SmallTest @SmallTest
public class TetheringConfigurationTest { public class TetheringConfigurationTest {
private final SharedLog mLog = new SharedLog("TetheringConfigurationTest");
@Mock private Context mContext; @Mock private Context mContext;
@Mock private TelephonyManager mTelephonyManager; @Mock private TelephonyManager mTelephonyManager;
@Mock private Resources mResources; @Mock private Resources mResources;
@@ -91,7 +93,7 @@ public class TetheringConfigurationTest {
mHasTelephonyManager = true; mHasTelephonyManager = true;
when(mTelephonyManager.getTetherApnRequired()).thenReturn(DUN_REQUIRED); when(mTelephonyManager.getTetherApnRequired()).thenReturn(DUN_REQUIRED);
final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext); final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext, mLog);
assertTrue(cfg.isDunRequired); assertTrue(cfg.isDunRequired);
assertTrue(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN)); assertTrue(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN));
assertFalse(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE)); assertFalse(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE));
@@ -107,7 +109,7 @@ public class TetheringConfigurationTest {
mHasTelephonyManager = true; mHasTelephonyManager = true;
when(mTelephonyManager.getTetherApnRequired()).thenReturn(DUN_NOT_REQUIRED); when(mTelephonyManager.getTetherApnRequired()).thenReturn(DUN_NOT_REQUIRED);
final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext); final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext, mLog);
assertFalse(cfg.isDunRequired); assertFalse(cfg.isDunRequired);
assertFalse(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN)); assertFalse(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN));
assertTrue(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE)); assertTrue(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE));
@@ -123,7 +125,7 @@ public class TetheringConfigurationTest {
mHasTelephonyManager = false; mHasTelephonyManager = false;
when(mTelephonyManager.getTetherApnRequired()).thenReturn(DUN_UNSPECIFIED); when(mTelephonyManager.getTetherApnRequired()).thenReturn(DUN_UNSPECIFIED);
final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext); final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext, mLog);
assertTrue(cfg.isDunRequired); assertTrue(cfg.isDunRequired);
assertTrue(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN)); assertTrue(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN));
// Just to prove we haven't clobbered Wi-Fi: // Just to prove we haven't clobbered Wi-Fi: