Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IP/Top: Add Android network interface
- Loading branch information
Showing
7 changed files
with
193 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/NetworkHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package org.dolphinemu.dolphinemu.utils; | ||
|
||
import android.app.Service; | ||
import android.content.Context; | ||
import android.net.ConnectivityManager; | ||
import android.net.LinkAddress; | ||
import android.net.RouteInfo; | ||
import android.os.Build; | ||
|
||
import androidx.annotation.RequiresApi; | ||
|
||
import org.dolphinemu.dolphinemu.NativeLibrary; | ||
|
||
import java.net.Inet4Address; | ||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.util.List; | ||
|
||
public class NetworkHelper | ||
{ | ||
private static ConnectivityManager GetConnectivityManager() | ||
{ | ||
Context context = NativeLibrary.getEmulationActivity(); | ||
if (context == null) | ||
{ | ||
Log.warning("Cannot get Network IP address as EmulationActivity is null."); | ||
return null; | ||
} | ||
ConnectivityManager manager = | ||
(ConnectivityManager) context.getSystemService(Service.CONNECTIVITY_SERVICE); | ||
if (manager == null) | ||
Log.warning("Cannot get Network link as ConnectivityManager is null."); | ||
return manager; | ||
} | ||
|
||
@RequiresApi(api = Build.VERSION_CODES.M) | ||
private static LinkAddress GetIPv4Link() | ||
{ | ||
ConnectivityManager manager = GetConnectivityManager(); | ||
if (manager == null) | ||
return null; | ||
List<LinkAddress> links = | ||
manager.getLinkProperties(manager.getActiveNetwork()).getLinkAddresses(); | ||
for (LinkAddress link : links) | ||
{ | ||
InetAddress address = link.getAddress(); | ||
if (address instanceof Inet4Address) | ||
return link; | ||
} | ||
Log.warning("No IPv4 link found."); | ||
return null; | ||
} | ||
|
||
private static int InetAddressToInt(InetAddress address) | ||
{ | ||
byte[] net_addr = address.getAddress(); | ||
int result = 0; | ||
// Convert address to little endian | ||
for (int i = 0; i < net_addr.length; i++) | ||
{ | ||
result |= (net_addr[i] & 0xFF) << (8 * i); | ||
} | ||
return result; | ||
} | ||
|
||
public static int GetNetworkIpAddress() | ||
{ | ||
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) | ||
return 0; | ||
LinkAddress link = GetIPv4Link(); | ||
if (link == null) | ||
return 0; | ||
return InetAddressToInt(link.getAddress()); | ||
} | ||
|
||
public static int GetNetworkPrefixLength() | ||
{ | ||
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) | ||
return 0; | ||
LinkAddress link = GetIPv4Link(); | ||
if (link == null) | ||
return 0; | ||
return link.getPrefixLength(); | ||
} | ||
|
||
public static int GetNetworkGateway() | ||
{ | ||
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) | ||
return 0; | ||
ConnectivityManager manager = GetConnectivityManager(); | ||
if (manager == null) | ||
return 0; | ||
List<RouteInfo> routes = manager.getLinkProperties(manager.getActiveNetwork()).getRoutes(); | ||
try | ||
{ | ||
InetAddress addr_out = InetAddress.getByName("8.8.8.8"); | ||
for (RouteInfo route : routes) | ||
{ | ||
if (!route.matches(addr_out)) | ||
continue; | ||
return InetAddressToInt(route.getGateway()); | ||
} | ||
} | ||
catch (UnknownHostException ignore) | ||
{ | ||
} | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters