diff --git a/app/src/full/java/cc/mvdan/accesspoint/WifiApControl.java b/app/src/full/java/cc/mvdan/accesspoint/WifiApControl.java index f2bee0a5e..07e746190 100644 --- a/app/src/full/java/cc/mvdan/accesspoint/WifiApControl.java +++ b/app/src/full/java/cc/mvdan/accesspoint/WifiApControl.java @@ -16,15 +16,14 @@ package cc.mvdan.accesspoint; -import android.annotation.SuppressLint; -import android.annotation.TargetApi; import android.content.Context; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; -import android.os.Build; import android.provider.Settings; import android.util.Log; +import org.fdroid.fdroid.BuildConfig; + import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; @@ -35,7 +34,6 @@ import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.util.ArrayList; -import java.util.Arrays; import java.util.Enumeration; import java.util.List; import java.util.concurrent.CountDownLatch; @@ -67,21 +65,28 @@ final public class WifiApControl { private static Method setWifiApEnabledMethod; static { - for (Method method : WifiManager.class.getDeclaredMethods()) { - switch (method.getName()) { - case "getWifiApConfiguration": - getWifiApConfigurationMethod = method; - break; - case "getWifiApState": - getWifiApStateMethod = method; - break; - case "isWifiApEnabled": - isWifiApEnabledMethod = method; - break; - case "setWifiApEnabled": - setWifiApEnabledMethod = method; - break; + try { + for (Method method : WifiManager.class.getDeclaredMethods()) { + switch (method.getName()) { + case "getWifiApConfiguration": + getWifiApConfigurationMethod = method; + break; + case "getWifiApState": + getWifiApStateMethod = method; + break; + case "isWifiApEnabled": + isWifiApEnabledMethod = method; + break; + case "setWifiApEnabled": + setWifiApEnabledMethod = method; + break; + } } + } catch (Exception e) { + if (BuildConfig.DEBUG) { + throw e; + } + Log.e(TAG, "WifiManager failed to init", e); } } @@ -134,7 +139,15 @@ final public class WifiApControl { Log.e(TAG, "6.0 or later, but haven't been granted WRITE_SETTINGS!"); return null; } - instance = new WifiApControl(context); + try { + instance = new WifiApControl(context); + instance.isEnabled(); // make sure this instance works + } catch (Exception e) { + if (BuildConfig.DEBUG) { + throw e; + } + Log.e(TAG, "WifiManager failed to init", e); + } } return instance; } @@ -144,15 +157,6 @@ final public class WifiApControl { return FALLBACK_DEVICE; } - private static byte[] macAddressToByteArray(String macString) { - String[] mac = macString.split("[:\\s-]"); - byte[] macAddress = new byte[6]; - for (int i = 0; i < mac.length; i++) { - macAddress[i] = Integer.decode("0x" + mac[i]).byteValue(); - } - return macAddress; - } - private static Object invokeQuietly(Method method, Object receiver, Object... args) { try { return method.invoke(receiver, args); diff --git a/app/src/full/java/org/fdroid/fdroid/nearby/WifiStateChangeService.java b/app/src/full/java/org/fdroid/fdroid/nearby/WifiStateChangeService.java index 11eeca152..e21ec99e5 100644 --- a/app/src/full/java/org/fdroid/fdroid/nearby/WifiStateChangeService.java +++ b/app/src/full/java/org/fdroid/fdroid/nearby/WifiStateChangeService.java @@ -261,13 +261,7 @@ public class WifiStateChangeService extends Worker { } FDroidApp.bssid = wifiInfo.getBSSID(); } else { - WifiApControl wifiApControl = null; - try { - wifiApControl = WifiApControl.getInstance(context); - wifiApControl.isEnabled(); - } catch (NullPointerException e) { - wifiApControl = null; - } + WifiApControl wifiApControl = WifiApControl.getInstance(context); Utils.debugLog(TAG, "WifiApControl: " + wifiApControl); if (wifiApControl == null && FDroidApp.ipAddressString != null) { wifiInfo = wifiManager.getConnectionInfo();