Minor improvement in logging in TetheringConfiguration

Test: as follows
    - built
    - booted
    - flashed
    - runtest frameworks-net passes
    - dumpsys connectivity shows expected output
Bug: 32163131
Bug: 36504926
Bug: 36988090
Bug: 38152109
Bug: 38186915
Bug: 38218697
Change-Id: I4a2129d780dfec7bca693486a100ea3c78465430
diff --git a/services/core/java/com/android/server/connectivity/Tethering.java b/services/core/java/com/android/server/connectivity/Tethering.java
index f212c80..0e2bc83 100644
--- a/services/core/java/com/android/server/connectivity/Tethering.java
+++ b/services/core/java/com/android/server/connectivity/Tethering.java
@@ -235,7 +235,7 @@
     }
 
     private void updateConfiguration() {
-        mConfig = new TetheringConfiguration(mContext);
+        mConfig = new TetheringConfiguration(mContext, mLog);
     }
 
     @Override
diff --git a/services/core/java/com/android/server/connectivity/tethering/TetheringConfiguration.java b/services/core/java/com/android/server/connectivity/tethering/TetheringConfiguration.java
index 6941193..651de89 100644
--- a/services/core/java/com/android/server/connectivity/tethering/TetheringConfiguration.java
+++ b/services/core/java/com/android/server/connectivity/tethering/TetheringConfiguration.java
@@ -25,7 +25,7 @@
 import android.content.res.Resources;
 import android.net.ConnectivityManager;
 import android.telephony.TelephonyManager;
-import android.util.Log;
+import android.net.util.SharedLog;
 
 import java.io.PrintWriter;
 import java.util.ArrayList;
@@ -74,7 +74,9 @@
     public final String[] dhcpRanges;
     public final String[] defaultIPv4DNS;
 
-    public TetheringConfiguration(Context ctx) {
+    public TetheringConfiguration(Context ctx, SharedLog log) {
+        final SharedLog configLog = log.forSubComponent("config");
+
         tetherableUsbRegexs = ctx.getResources().getStringArray(
                 com.android.internal.R.array.config_tether_usb_regexs);
         tetherableWifiRegexs = ctx.getResources().getStringArray(
@@ -83,11 +85,15 @@
                 com.android.internal.R.array.config_tether_bluetooth_regexs);
 
         final int dunCheck = checkDunRequired(ctx);
+        configLog.log("DUN check returned: " + dunCheckString(dunCheck));
+
         preferredUpstreamIfaceTypes = getUpstreamIfaceTypes(ctx, dunCheck);
         isDunRequired = preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN);
 
         dhcpRanges = getDhcpRanges(ctx);
         defaultIPv4DNS = copy(DEFAULT_IPV4_DNS);
+
+        configLog.log(toString());
     }
 
     public boolean isUsb(String iface) {
@@ -110,21 +116,25 @@
         pw.print("isDunRequired: ");
         pw.println(isDunRequired);
 
-        String[] upstreamTypes = null;
-        if (preferredUpstreamIfaceTypes != null) {
-            upstreamTypes = new String[preferredUpstreamIfaceTypes.size()];
-            int i = 0;
-            for (Integer netType : preferredUpstreamIfaceTypes) {
-                upstreamTypes[i] = ConnectivityManager.getNetworkTypeName(netType);
-                i++;
-            }
-        }
-        dumpStringArray(pw, "preferredUpstreamIfaceTypes", upstreamTypes);
+        dumpStringArray(pw, "preferredUpstreamIfaceTypes",
+                preferredUpstreamNames(preferredUpstreamIfaceTypes));
 
         dumpStringArray(pw, "dhcpRanges", dhcpRanges);
         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) {
         pw.print(label);
         pw.print(": ");
@@ -140,11 +150,42 @@
         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) {
         final TelephonyManager tm = (TelephonyManager) ctx.getSystemService(TELEPHONY_SERVICE);
         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) {
         final int ifaceTypes[] = ctx.getResources().getIntArray(
                 com.android.internal.R.array.config_tether_upstream_types);
diff --git a/tests/net/java/com/android/server/connectivity/tethering/TetheringConfigurationTest.java b/tests/net/java/com/android/server/connectivity/tethering/TetheringConfigurationTest.java
index ddceea2..27be135 100644
--- a/tests/net/java/com/android/server/connectivity/tethering/TetheringConfigurationTest.java
+++ b/tests/net/java/com/android/server/connectivity/tethering/TetheringConfigurationTest.java
@@ -31,6 +31,7 @@
 import android.content.Context;
 import android.content.ContextWrapper;
 import android.content.res.Resources;
+import android.net.util.SharedLog;
 import android.support.test.filters.SmallTest;
 import android.support.test.runner.AndroidJUnit4;
 import android.telephony.TelephonyManager;
@@ -47,6 +48,7 @@
 @RunWith(AndroidJUnit4.class)
 @SmallTest
 public class TetheringConfigurationTest {
+    private final SharedLog mLog = new SharedLog("TetheringConfigurationTest");
     @Mock private Context mContext;
     @Mock private TelephonyManager mTelephonyManager;
     @Mock private Resources mResources;
@@ -91,7 +93,7 @@
         mHasTelephonyManager = true;
         when(mTelephonyManager.getTetherApnRequired()).thenReturn(DUN_REQUIRED);
 
-        final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext);
+        final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext, mLog);
         assertTrue(cfg.isDunRequired);
         assertTrue(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN));
         assertFalse(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE));
@@ -107,7 +109,7 @@
         mHasTelephonyManager = true;
         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.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN));
         assertTrue(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE));
@@ -123,7 +125,7 @@
         mHasTelephonyManager = false;
         when(mTelephonyManager.getTetherApnRequired()).thenReturn(DUN_UNSPECIFIED);
 
-        final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext);
+        final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext, mLog);
         assertTrue(cfg.isDunRequired);
         assertTrue(cfg.preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN));
         // Just to prove we haven't clobbered Wi-Fi: