Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5d654818 authored by Erik Kline's avatar Erik Kline Committed by android-build-merger
Browse files

Merge "Minor improvement in logging in TetheringConfiguration" am: 39ce589f...

Merge "Minor improvement in logging in TetheringConfiguration" am: 39ce589f am: 8211c36d am: 13159034
am: 3f5f5510

Change-Id: Iccfbc1a3d0bc55c4fb2188a06a31ec3eaf584547
parents faed8eb2 3f5f5510
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -238,7 +238,7 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
    }

    private void updateConfiguration() {
        mConfig = new TetheringConfiguration(mContext);
        mConfig = new TetheringConfiguration(mContext, mLog);
    }

    @Override
+53 −12
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ import android.content.Context;
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 class TetheringConfiguration {
    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 @@ public class TetheringConfiguration {
                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 @@ public class TetheringConfiguration {
        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 @@ public class TetheringConfiguration {
        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);
+5 −3
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import static org.mockito.Mockito.when;
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 @@ import org.mockito.MockitoAnnotations;
@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 @@ public class TetheringConfigurationTest {
        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 @@ public class TetheringConfigurationTest {
        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 @@ public class TetheringConfigurationTest {
        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: