pull/21/head
FongMi 4 years ago
parent 87be5e0cf0
commit 700994677c
  1. 38
      app/src/main/java/com/fongmi/android/tv/server/Server.java
  2. 35
      app/src/main/java/com/fongmi/android/tv/utils/Utils.java

@ -1,10 +1,20 @@
package com.fongmi.android.tv.server;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.text.format.Formatter;
import com.fongmi.android.tv.App;
import com.fongmi.android.tv.event.ServerEvent;
import com.fongmi.android.tv.utils.Utils;
import org.greenrobot.eventbus.EventBus;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class Server implements Nano.Listener {
private Nano nano;
@ -23,7 +33,7 @@ public class Server implements Nano.Listener {
}
public String getAddress(boolean local) {
return "http://" + (local ? "127.0.0.1" : Utils.getIP()) + ":" + port;
return "http://" + (local ? "127.0.0.1" : getIP()) + ":" + port;
}
public void start() {
@ -49,6 +59,30 @@ public class Server implements Nano.Listener {
}
}
private String getIP() {
WifiManager manager = (WifiManager) App.get().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
int address = manager.getConnectionInfo().getIpAddress();
if (address != 0) return Formatter.formatIpAddress(address);
try {
return getHostAddress();
} catch (SocketException e) {
return "";
}
}
private String getHostAddress() throws SocketException {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface interfaces = en.nextElement();
for (Enumeration<InetAddress> addresses = interfaces.getInetAddresses(); addresses.hasMoreElements(); ) {
InetAddress inetAddress = addresses.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
return "";
}
@Override
public void onSearch(String text) {
EventBus.getDefault().post(ServerEvent.search(text));

@ -2,23 +2,15 @@ package com.fongmi.android.tv.utils;
import android.app.Activity;
import android.app.PictureInPictureParams;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.text.format.Formatter;
import android.util.Rational;
import android.view.View;
import com.fongmi.android.tv.App;
import com.google.android.exoplayer2.util.Util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.regex.Pattern;
public class Utils {
@ -45,31 +37,6 @@ public class Utils {
activity.getWindow().getDecorView().setSystemUiVisibility(flags);
}
public static String getIP() {
WifiManager manager = (WifiManager) App.get().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
int ipAddress = manager.getConnectionInfo().getIpAddress();
if (ipAddress != 0) return Formatter.formatIpAddress(manager.getConnectionInfo().getIpAddress());
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface element = interfaces.nextElement();
String interfaceName = element.getDisplayName();
if (interfaceName.equals("eth0") || interfaceName.equals("wlan0")) {
Enumeration<InetAddress> addresses = element.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
if (!address.isLoopbackAddress() && address instanceof Inet4Address) {
return address.getHostAddress();
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return "0.0.0.0";
}
public static String getUUID() {
return Settings.Secure.getString(App.get().getContentResolver(), Settings.Secure.ANDROID_ID);
}
@ -83,6 +50,4 @@ public class Utils {
if (SNIFFER.matcher(url).find()) return !url.contains("cdn-tos") || (!url.contains(".js") && !url.contains(".css"));
return false;
}
}

Loading…
Cancel
Save