From 3cbccdb6c39a3b66a003596dcdbaff9c2a0d526b Mon Sep 17 00:00:00 2001 From: Tomasz Wasilczyk Date: Tue, 26 Jan 2021 08:46:14 -0800 Subject: [PATCH] Don't crash Tethering service when WiFi feature is missing Bug: 175430552 Test: disable WiFi feature on device with wlan0 Change-Id: Ie8b422ed6e0a0a98eb1c7e6072464859d9083ba5 --- .../networkstack/tethering/Tethering.java | 25 +++++++++++++++---- .../networkstack/tethering/TetheringTest.java | 3 +++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Tethering/src/com/android/networkstack/tethering/Tethering.java b/Tethering/src/com/android/networkstack/tethering/Tethering.java index 385c691025..ac5857d1c8 100644 --- a/Tethering/src/com/android/networkstack/tethering/Tethering.java +++ b/Tethering/src/com/android/networkstack/tethering/Tethering.java @@ -1689,12 +1689,14 @@ public class Tethering { // If this is a Wi-Fi interface, tell WifiManager of any errors // or the inactive serving state. if (who.interfaceType() == TETHERING_WIFI) { - if (who.lastError() != TETHER_ERROR_NO_ERROR) { - getWifiManager().updateInterfaceIpState( - who.interfaceName(), IFACE_IP_MODE_CONFIGURATION_ERROR); + final WifiManager mgr = getWifiManager(); + final String iface = who.interfaceName(); + if (mgr == null) { + Log.wtf(TAG, "Skipping WifiManager notification about inactive tethering"); + } else if (who.lastError() != TETHER_ERROR_NO_ERROR) { + mgr.updateInterfaceIpState(iface, IFACE_IP_MODE_CONFIGURATION_ERROR); } else { - getWifiManager().updateInterfaceIpState( - who.interfaceName(), IFACE_IP_MODE_UNSPECIFIED); + mgr.updateInterfaceIpState(iface, IFACE_IP_MODE_UNSPECIFIED); } } } @@ -2421,6 +2423,19 @@ public class Tethering { mLog.log(iface + " is not a tetherable iface, ignoring"); return; } + + final PackageManager pm = mContext.getPackageManager(); + if ((interfaceType == TETHERING_WIFI || interfaceType == TETHERING_WIGIG) + && !pm.hasSystemFeature(PackageManager.FEATURE_WIFI)) { + mLog.log(iface + " is not tetherable, because WiFi feature is disabled"); + return; + } + if (interfaceType == TETHERING_WIFI_P2P + && !pm.hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT)) { + mLog.log(iface + " is not tetherable, because WiFi Direct feature is disabled"); + return; + } + maybeTrackNewInterfaceLocked(iface, interfaceType); } diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java index f4b3749132..60fddb51ab 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java @@ -586,6 +586,9 @@ public class TetheringTest { ArgumentCaptor.forClass(SoftApCallback.class); verify(mWifiManager).registerSoftApCallback(any(), softApCallbackCaptor.capture()); mSoftApCallback = softApCallbackCaptor.getValue(); + + when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WIFI)).thenReturn(true); + when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT)).thenReturn(true); } private void setTetheringSupported(final boolean supported) {