diff --git a/src/main/java/org/swiftp/FTPServerService.java b/src/main/java/org/swiftp/FTPServerService.java index e426a2f2508d777c63fbc0b948749d66dce7da29..3a1bc4444dc3bc0f9e5908cfad83ed56990cf24a 100644 --- a/src/main/java/org/swiftp/FTPServerService.java +++ b/src/main/java/org/swiftp/FTPServerService.java @@ -21,10 +21,14 @@ package org.swiftp; import java.io.File; import java.io.IOException; +import java.lang.reflect.Method; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.net.NetworkInterface; import java.net.ServerSocket; +import java.net.SocketException; import java.util.ArrayList; +import java.util.Collections; import java.util.Date; import java.util.List; @@ -63,6 +67,7 @@ public abstract class FTPServerService extends Service implements Runnable { // because we cannot wait forever in accept() if we want to be able // to receive an exit signal and cleanly exit. public static final int WAKE_INTERVAL_MS = 1000; // milliseconds + private static final int WIFI_AP_STATE_ENABLED = 13; protected static Thread serverThread = null; protected static MyLog staticLog = new MyLog(FTPServerService.class.getName()); protected static WifiLock wifiLock = null; @@ -126,6 +131,27 @@ public abstract class FTPServerService extends Service implements Runnable { } } + public static InetAddress getWifiAndApIp(){ + InetAddress ip = getWifiIp(); + if (ip==null){ + try { + for (NetworkInterface intf : Collections.list(NetworkInterface.getNetworkInterfaces())) { + if (intf.getName().equals("wlan0")){ + for (InetAddress addr : Collections.list(intf.getInetAddresses())) { + if (!addr.isLoopbackAddress() && addr.getHostAddress().contains(".")){ + ip = addr; + break; + } + } + break; + } + } + } catch (SocketException ignored) { + } + } + return ip; + } + public static boolean isWifiEnabled() { Context myContext = Globals.getContext(); if (myContext == null) { @@ -140,6 +166,25 @@ public abstract class FTPServerService extends Service implements Runnable { } } + public static boolean isWifiAndApEnabled(){ + Context myContext = Globals.getContext(); + if (myContext == null) { + throw new NullPointerException("Global context is null"); + } + @SuppressLint("WifiManagerLeak") WifiManager wifiMgr = (WifiManager) myContext + .getSystemService(Context.WIFI_SERVICE); + if (wifiMgr.getWifiState() == WifiManager.WIFI_STATE_ENABLED) { + return true; + } else { + try { + Method method = wifiMgr.getClass().getMethod("getWifiApState"); + return (Integer) method.invoke(wifiMgr) == WIFI_AP_STATE_ENABLED; + } catch (Exception e){ + return false; + } + } + } + public static List getSessionMonitorContents() { return new ArrayList(sessionMonitor); } @@ -398,7 +443,7 @@ public abstract class FTPServerService extends Service implements Runnable { return; } - if (!isWifiEnabled()) { + if (!isWifiAndApEnabled()) { cleanupAndStopService(); sendBroadcast(new Intent(ACTION_FAILEDTOSTART)); return; diff --git a/src/main/java/org/swiftp/gui/ServerPreferenceFragment.java b/src/main/java/org/swiftp/gui/ServerPreferenceFragment.java index 0d9440316d61b0cca993c560e7f2163a65b5e3ea..ddff8f1c6d4e9c7e3cf3dbd056cfbe0053bfe5f5 100644 --- a/src/main/java/org/swiftp/gui/ServerPreferenceFragment.java +++ b/src/main/java/org/swiftp/gui/ServerPreferenceFragment.java @@ -49,7 +49,7 @@ public class ServerPreferenceFragment extends PreferenceFragment implements if (intent.getAction().equals(FTPServerService.ACTION_STARTED)) { running_state.setChecked(true); // Fill in the FTP server address - InetAddress address = FTPServerService.getWifiIp(); + InetAddress address = FTPServerService.getWifiAndApIp(); if (address == null) { Log.v(TAG, "Unable to retreive wifi ip address"); running_state.setSummary(R.string.cant_get_url);