mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-11-20 08:21:46 +00:00
Merge cdc21490e4 into 0fdf1cc386
This commit is contained in:
commit
b5b7f23ef6
@ -1,127 +0,0 @@
|
|||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
||||||
|
|
||||||
package org.dolphinemu.dolphinemu.utils;
|
|
||||||
|
|
||||||
import android.app.Service;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.net.ConnectivityManager;
|
|
||||||
import android.net.LinkAddress;
|
|
||||||
import android.net.LinkProperties;
|
|
||||||
import android.net.Network;
|
|
||||||
import android.net.RouteInfo;
|
|
||||||
import android.os.Build;
|
|
||||||
|
|
||||||
import androidx.annotation.Keep;
|
|
||||||
import androidx.annotation.RequiresApi;
|
|
||||||
|
|
||||||
import org.dolphinemu.dolphinemu.DolphinApplication;
|
|
||||||
|
|
||||||
import java.net.Inet4Address;
|
|
||||||
import java.net.InetAddress;
|
|
||||||
import java.net.UnknownHostException;
|
|
||||||
|
|
||||||
public class NetworkHelper
|
|
||||||
{
|
|
||||||
private static ConnectivityManager GetConnectivityManager()
|
|
||||||
{
|
|
||||||
Context context = DolphinApplication.getAppContext();
|
|
||||||
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;
|
|
||||||
Network active_network = manager.getActiveNetwork();
|
|
||||||
if (active_network == null)
|
|
||||||
{
|
|
||||||
Log.warning("Active network is null.");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
LinkProperties properties = manager.getLinkProperties(active_network);
|
|
||||||
if (properties == null)
|
|
||||||
{
|
|
||||||
Log.warning("Link properties is null.");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
for (LinkAddress link : properties.getLinkAddresses())
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Keep
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Keep
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Keep
|
|
||||||
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;
|
|
||||||
Network active_network = manager.getActiveNetwork();
|
|
||||||
if (active_network == null)
|
|
||||||
return 0;
|
|
||||||
LinkProperties properties = manager.getLinkProperties(active_network);
|
|
||||||
if (properties == null)
|
|
||||||
return 0;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
InetAddress addr_out = InetAddress.getByName("8.8.8.8");
|
|
||||||
for (RouteInfo route : properties.getRoutes())
|
|
||||||
{
|
|
||||||
if (!route.matches(addr_out))
|
|
||||||
continue;
|
|
||||||
return InetAddressToInt(route.getGateway());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (UnknownHostException ignore)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
Log.warning("No valid gateway found.");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,84 @@
|
|||||||
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
|
package org.dolphinemu.dolphinemu.utils
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.net.ConnectivityManager
|
||||||
|
import android.net.LinkAddress
|
||||||
|
import android.os.Build
|
||||||
|
import androidx.annotation.Keep
|
||||||
|
import androidx.annotation.RequiresApi
|
||||||
|
import org.dolphinemu.dolphinemu.DolphinApplication
|
||||||
|
import java.net.Inet4Address
|
||||||
|
import java.net.InetAddress
|
||||||
|
import java.net.UnknownHostException
|
||||||
|
|
||||||
|
@Keep
|
||||||
|
object NetworkHelper {
|
||||||
|
private fun getConnectivityManager(): ConnectivityManager? {
|
||||||
|
val context = DolphinApplication.getAppContext()
|
||||||
|
val manager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager
|
||||||
|
if (manager == null) {
|
||||||
|
Log.warning("Cannot get network link as ConnectivityManager is null.")
|
||||||
|
}
|
||||||
|
return manager
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiresApi(Build.VERSION_CODES.M)
|
||||||
|
private fun getIPv4Link(): LinkAddress? {
|
||||||
|
val manager = getConnectivityManager() ?: return null
|
||||||
|
val activeNetwork = manager.activeNetwork
|
||||||
|
if (activeNetwork == null) {
|
||||||
|
Log.warning("Active network is null.")
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
val properties = manager.getLinkProperties(activeNetwork)
|
||||||
|
if (properties == null) {
|
||||||
|
Log.warning("Link properties is null.")
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
return properties.linkAddresses.firstOrNull { it.address is Inet4Address } ?: run {
|
||||||
|
Log.warning("No IPv4 link found.")
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun InetAddress.inetAddressToInt(): Int {
|
||||||
|
return address.foldIndexed(0) { index, acc, byte ->
|
||||||
|
acc or ((byte.toInt() and 0xFF) shl (8 * index))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Keep
|
||||||
|
@JvmStatic
|
||||||
|
fun getNetworkIpAddress(): Int {
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return 0
|
||||||
|
return getIPv4Link()?.address?.inetAddressToInt() ?: 0
|
||||||
|
}
|
||||||
|
|
||||||
|
@Keep
|
||||||
|
@JvmStatic
|
||||||
|
fun getNetworkPrefixLength(): Int {
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return 0
|
||||||
|
return getIPv4Link()?.prefixLength ?: 0
|
||||||
|
}
|
||||||
|
|
||||||
|
@Keep
|
||||||
|
@JvmStatic
|
||||||
|
fun getNetworkGateway(): Int {
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return 0
|
||||||
|
val manager = getConnectivityManager() ?: return 0
|
||||||
|
val activeNetwork = manager.activeNetwork ?: return 0
|
||||||
|
val properties = manager.getLinkProperties(activeNetwork) ?: return 0
|
||||||
|
val gatewayAddress = try {
|
||||||
|
val target = InetAddress.getByName("8.8.8.8")
|
||||||
|
properties.routes.firstOrNull { it.matches(target) }?.gateway
|
||||||
|
} catch (ignored: UnknownHostException) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
return gatewayAddress?.inetAddressToInt() ?: run {
|
||||||
|
Log.warning("No valid gateway found.")
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -662,11 +662,11 @@ JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
|
|||||||
env->FindClass("org/dolphinemu/dolphinemu/utils/NetworkHelper");
|
env->FindClass("org/dolphinemu/dolphinemu/utils/NetworkHelper");
|
||||||
s_network_helper_class = reinterpret_cast<jclass>(env->NewGlobalRef(network_helper_class));
|
s_network_helper_class = reinterpret_cast<jclass>(env->NewGlobalRef(network_helper_class));
|
||||||
s_network_helper_get_network_ip_address =
|
s_network_helper_get_network_ip_address =
|
||||||
env->GetStaticMethodID(s_network_helper_class, "GetNetworkIpAddress", "()I");
|
env->GetStaticMethodID(s_network_helper_class, "getNetworkIpAddress", "()I");
|
||||||
s_network_helper_get_network_prefix_length =
|
s_network_helper_get_network_prefix_length =
|
||||||
env->GetStaticMethodID(s_network_helper_class, "GetNetworkPrefixLength", "()I");
|
env->GetStaticMethodID(s_network_helper_class, "getNetworkPrefixLength", "()I");
|
||||||
s_network_helper_get_network_gateway =
|
s_network_helper_get_network_gateway =
|
||||||
env->GetStaticMethodID(s_network_helper_class, "GetNetworkGateway", "()I");
|
env->GetStaticMethodID(s_network_helper_class, "getNetworkGateway", "()I");
|
||||||
env->DeleteLocalRef(network_helper_class);
|
env->DeleteLocalRef(network_helper_class);
|
||||||
|
|
||||||
const jclass boolean_supplier_class =
|
const jclass boolean_supplier_class =
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user