Compare commits

...

6 Commits

Author SHA1 Message Date
Simonx22
b5b7f23ef6
Merge cdc21490e4 into 0fdf1cc386 2025-11-15 10:24:26 -05:00
JosJuice
0fdf1cc386
Merge pull request #14112 from Simonx22/android/remove-unused-bimap-class
Android: Remove unused BiMap class
2025-11-15 16:22:17 +01:00
JosJuice
999e13b3b3
Merge pull request #14109 from OatmealDome/analytics-deadlock
DolphinAnalytics: Only call ReloadConfig in config changed callback when analytics enabled value changes
2025-11-15 14:44:54 +01:00
Simonx22
d1526157df Android: Remove unused BiMap class 2025-11-12 17:26:05 -05:00
OatmealDome
df5f351add
DolphinAnalytics: Only call ReloadConfig in config changed callback when analytics enabled value changes
Co-authored-by: Simonx22 <simon@oatmealdome.me>
2025-11-12 00:09:41 -05:00
Simonx22
cdc21490e4 Android: Convert NetworkHelper to Kotlin 2025-11-11 15:58:07 -05:00
6 changed files with 99 additions and 161 deletions

View File

@ -1,29 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.utils;
import java.util.HashMap;
import java.util.Map;
public class BiMap<K, V>
{
private Map<K, V> forward = new HashMap<>();
private Map<V, K> backward = new HashMap<>();
public synchronized void add(K key, V value)
{
forward.put(key, value);
backward.put(value, key);
}
public synchronized V getForward(K key)
{
return forward.get(key);
}
public synchronized K getBackward(V key)
{
return backward.get(key);
}
}

View File

@ -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;
}
}

View File

@ -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
}
}
}

View File

@ -662,11 +662,11 @@ JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
env->FindClass("org/dolphinemu/dolphinemu/utils/NetworkHelper");
s_network_helper_class = reinterpret_cast<jclass>(env->NewGlobalRef(network_helper_class));
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 =
env->GetStaticMethodID(s_network_helper_class, "GetNetworkPrefixLength", "()I");
env->GetStaticMethodID(s_network_helper_class, "getNetworkPrefixLength", "()I");
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);
const jclass boolean_supplier_class =

View File

@ -57,10 +57,19 @@ void DolphinAnalytics::AndroidSetGetValFunc(std::function<std::string(std::strin
DolphinAnalytics::DolphinAnalytics()
{
m_last_analytics_enabled = Config::Get(Config::MAIN_ANALYTICS_ENABLED);
ReloadConfig();
MakeBaseBuilder();
m_config_changed_callback_id = Config::AddConfigChangedCallback([this] { ReloadConfig(); });
m_config_changed_callback_id = Config::AddConfigChangedCallback([this] {
bool current_analytics_enabled = Config::Get(Config::MAIN_ANALYTICS_ENABLED);
if (m_last_analytics_enabled != current_analytics_enabled)
{
m_last_analytics_enabled = current_analytics_enabled;
ReloadConfig();
}
});
}
DolphinAnalytics::~DolphinAnalytics()
@ -80,7 +89,7 @@ void DolphinAnalytics::ReloadConfig()
// Install the HTTP backend if analytics support is enabled.
std::unique_ptr<Common::AnalyticsReportingBackend> new_backend;
if (Config::Get(Config::MAIN_ANALYTICS_ENABLED))
if (m_last_analytics_enabled)
{
new_backend = std::make_unique<Common::HttpAnalyticsBackend>(ANALYTICS_ENDPOINT);
}

View File

@ -203,4 +203,5 @@ private:
std::mutex m_reporter_mutex;
Common::AnalyticsReporter m_reporter;
Config::ConfigChangedCallbackID m_config_changed_callback_id{};
bool m_last_analytics_enabled = false;
};