2020-07-06 17:02:01 +02:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-07-06 17:02:01 +02:00
|
|
|
|
|
|
|
#include "jni/AndroidCommon/AndroidCommon.h"
|
|
|
|
|
2021-10-13 17:39:09 +02:00
|
|
|
#include <algorithm>
|
2020-11-04 20:59:39 +01:00
|
|
|
#include <ios>
|
2020-07-06 17:02:01 +02:00
|
|
|
#include <string>
|
2020-06-28 19:16:23 +02:00
|
|
|
#include <string_view>
|
2020-07-06 17:02:01 +02:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <jni.h>
|
|
|
|
|
2020-11-04 20:59:39 +01:00
|
|
|
#include "Common/Assert.h"
|
2020-06-28 19:16:23 +02:00
|
|
|
#include "Common/StringUtil.h"
|
2020-06-26 17:52:31 +02:00
|
|
|
#include "jni/AndroidCommon/IDCache.h"
|
2020-06-28 19:16:23 +02:00
|
|
|
|
2020-07-06 17:02:01 +02:00
|
|
|
std::string GetJString(JNIEnv* env, jstring jstr)
|
|
|
|
{
|
2020-06-28 19:16:23 +02:00
|
|
|
const jchar* jchars = env->GetStringChars(jstr, nullptr);
|
|
|
|
const jsize length = env->GetStringLength(jstr);
|
|
|
|
const std::u16string_view string_view(reinterpret_cast<const char16_t*>(jchars), length);
|
2022-12-27 22:28:06 +01:00
|
|
|
std::string converted_string = UTF16ToUTF8(string_view);
|
2020-06-28 19:16:23 +02:00
|
|
|
env->ReleaseStringChars(jstr, jchars);
|
|
|
|
return converted_string;
|
2020-07-06 17:02:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
jstring ToJString(JNIEnv* env, const std::string& str)
|
|
|
|
{
|
2020-06-28 19:16:23 +02:00
|
|
|
const std::u16string converted_string = UTF8ToUTF16(str);
|
|
|
|
return env->NewString(reinterpret_cast<const jchar*>(converted_string.data()),
|
|
|
|
converted_string.size());
|
2020-07-06 17:02:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> JStringArrayToVector(JNIEnv* env, jobjectArray array)
|
|
|
|
{
|
|
|
|
const jsize size = env->GetArrayLength(array);
|
|
|
|
std::vector<std::string> result;
|
|
|
|
result.reserve(size);
|
|
|
|
|
|
|
|
for (jsize i = 0; i < size; ++i)
|
2021-05-24 22:01:49 +02:00
|
|
|
{
|
|
|
|
jstring str = reinterpret_cast<jstring>(env->GetObjectArrayElement(array, i));
|
|
|
|
result.push_back(GetJString(env, str));
|
|
|
|
env->DeleteLocalRef(str);
|
|
|
|
}
|
2020-07-06 17:02:01 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2020-06-26 17:52:31 +02:00
|
|
|
|
2022-12-27 22:28:06 +01:00
|
|
|
jobjectArray VectorToJStringArray(JNIEnv* env, const std::vector<std::string>& vector)
|
2020-12-28 13:25:24 +01:00
|
|
|
{
|
2022-12-27 22:28:06 +01:00
|
|
|
return VectorToJObjectArray(env, vector, IDCache::GetStringClass(), ToJString);
|
2020-12-28 13:25:24 +01:00
|
|
|
}
|
|
|
|
|
2020-11-04 20:59:39 +01:00
|
|
|
bool IsPathAndroidContent(const std::string& uri)
|
2020-06-26 17:52:31 +02:00
|
|
|
{
|
2023-01-24 14:25:49 -05:00
|
|
|
return uri.starts_with("content://");
|
2020-11-04 20:59:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string OpenModeToAndroid(std::string mode)
|
|
|
|
{
|
2021-05-24 22:01:49 +02:00
|
|
|
// The 'b' specifier is not supported by Android. Since we're on POSIX, it's fine to just skip it.
|
2021-10-13 17:39:09 +02:00
|
|
|
mode.erase(std::remove(mode.begin(), mode.end(), 'b'));
|
2020-06-26 17:52:31 +02:00
|
|
|
|
2020-11-04 20:59:39 +01:00
|
|
|
if (mode == "r+")
|
|
|
|
mode = "rw";
|
|
|
|
else if (mode == "w+")
|
|
|
|
mode = "rwt";
|
|
|
|
else if (mode == "a+")
|
|
|
|
mode = "rwa";
|
|
|
|
else if (mode == "a")
|
|
|
|
mode = "wa";
|
2020-06-26 17:52:31 +02:00
|
|
|
|
2020-11-04 20:59:39 +01:00
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2020-11-04 20:59:39 +01:00
|
|
|
std::string OpenModeToAndroid(std::ios_base::openmode mode)
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
if (mode & std::ios_base::in)
|
|
|
|
result += 'r';
|
|
|
|
|
|
|
|
if (mode & (std::ios_base::out | std::ios_base::app))
|
|
|
|
result += 'w';
|
|
|
|
|
|
|
|
if (mode & std::ios_base::app)
|
|
|
|
result += 'a';
|
|
|
|
|
|
|
|
constexpr std::ios_base::openmode t = std::ios_base::in | std::ios_base::trunc;
|
|
|
|
if ((mode & t) == t)
|
|
|
|
result += 't';
|
|
|
|
|
2021-05-24 22:01:49 +02:00
|
|
|
// The 'b' specifier is not supported by Android. Since we're on POSIX, it's fine to just skip it.
|
2020-11-04 20:59:39 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-06-26 17:52:31 +02:00
|
|
|
int OpenAndroidContent(const std::string& uri, const std::string& mode)
|
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
2022-12-25 16:30:51 +01:00
|
|
|
|
|
|
|
jstring j_uri = ToJString(env, uri);
|
|
|
|
jstring j_mode = ToJString(env, mode);
|
|
|
|
|
|
|
|
jint result = env->CallStaticIntMethod(IDCache::GetContentHandlerClass(),
|
|
|
|
IDCache::GetContentHandlerOpenFd(), j_uri, j_mode);
|
|
|
|
|
|
|
|
env->DeleteLocalRef(j_uri);
|
|
|
|
env->DeleteLocalRef(j_mode);
|
|
|
|
|
|
|
|
return result;
|
2020-06-26 17:52:31 +02:00
|
|
|
}
|
2020-06-27 11:04:48 +02:00
|
|
|
|
|
|
|
bool DeleteAndroidContent(const std::string& uri)
|
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
2022-12-25 16:30:51 +01:00
|
|
|
|
|
|
|
jstring j_uri = ToJString(env, uri);
|
|
|
|
|
|
|
|
jboolean result = env->CallStaticBooleanMethod(IDCache::GetContentHandlerClass(),
|
|
|
|
IDCache::GetContentHandlerDelete(), j_uri);
|
|
|
|
|
|
|
|
env->DeleteLocalRef(j_uri);
|
|
|
|
|
|
|
|
return static_cast<bool>(result);
|
2020-06-27 11:04:48 +02:00
|
|
|
}
|
2020-10-01 12:10:34 +04:00
|
|
|
|
2020-11-05 19:47:23 +01:00
|
|
|
jlong GetAndroidContentSizeAndIsDirectory(const std::string& uri)
|
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
2022-12-25 16:30:51 +01:00
|
|
|
|
|
|
|
jstring j_uri = ToJString(env, uri);
|
|
|
|
|
|
|
|
jlong result = env->CallStaticLongMethod(
|
|
|
|
IDCache::GetContentHandlerClass(), IDCache::GetContentHandlerGetSizeAndIsDirectory(), j_uri);
|
|
|
|
|
|
|
|
env->DeleteLocalRef(j_uri);
|
|
|
|
|
|
|
|
return result;
|
2020-11-05 19:47:23 +01:00
|
|
|
}
|
|
|
|
|
2020-11-08 15:39:17 +01:00
|
|
|
std::string GetAndroidContentDisplayName(const std::string& uri)
|
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
2021-05-24 22:01:49 +02:00
|
|
|
|
2022-12-25 16:30:51 +01:00
|
|
|
jstring j_uri = ToJString(env, uri);
|
2021-05-24 22:01:49 +02:00
|
|
|
|
2022-12-25 16:30:51 +01:00
|
|
|
jstring j_result = reinterpret_cast<jstring>(env->CallStaticObjectMethod(
|
|
|
|
IDCache::GetContentHandlerClass(), IDCache::GetContentHandlerGetDisplayName(), j_uri));
|
|
|
|
|
|
|
|
env->DeleteLocalRef(j_uri);
|
|
|
|
|
|
|
|
if (!j_result)
|
2021-05-24 22:01:49 +02:00
|
|
|
return "";
|
|
|
|
|
2022-12-25 16:30:51 +01:00
|
|
|
std::string result = GetJString(env, j_result);
|
2021-05-24 22:01:49 +02:00
|
|
|
|
2022-12-25 16:30:51 +01:00
|
|
|
env->DeleteLocalRef(j_result);
|
2021-05-24 22:01:49 +02:00
|
|
|
|
|
|
|
return result;
|
2020-11-08 15:39:17 +01:00
|
|
|
}
|
|
|
|
|
2020-11-08 16:57:49 +01:00
|
|
|
std::vector<std::string> GetAndroidContentChildNames(const std::string& uri)
|
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
2021-05-24 22:01:49 +02:00
|
|
|
|
2022-12-25 16:30:51 +01:00
|
|
|
jstring j_uri = ToJString(env, uri);
|
2021-05-24 22:01:49 +02:00
|
|
|
|
2022-12-25 16:30:51 +01:00
|
|
|
jobjectArray j_result = reinterpret_cast<jobjectArray>(env->CallStaticObjectMethod(
|
|
|
|
IDCache::GetContentHandlerClass(), IDCache::GetContentHandlerGetChildNames(), j_uri, false));
|
2021-05-24 22:01:49 +02:00
|
|
|
|
2022-12-25 16:30:51 +01:00
|
|
|
std::vector<std::string> result = JStringArrayToVector(env, j_result);
|
|
|
|
|
|
|
|
env->DeleteLocalRef(j_uri);
|
|
|
|
env->DeleteLocalRef(j_result);
|
2021-05-24 22:01:49 +02:00
|
|
|
|
|
|
|
return result;
|
2020-11-08 16:57:49 +01:00
|
|
|
}
|
|
|
|
|
2020-12-28 13:25:24 +01:00
|
|
|
std::vector<std::string> DoFileSearchAndroidContent(const std::string& directory,
|
|
|
|
const std::vector<std::string>& extensions,
|
|
|
|
bool recursive)
|
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
2021-05-24 22:01:49 +02:00
|
|
|
|
2022-12-25 16:30:51 +01:00
|
|
|
jstring j_directory = ToJString(env, directory);
|
|
|
|
jobjectArray j_extensions = VectorToJStringArray(env, extensions);
|
|
|
|
|
|
|
|
jobjectArray j_result = reinterpret_cast<jobjectArray>(env->CallStaticObjectMethod(
|
|
|
|
IDCache::GetContentHandlerClass(), IDCache::GetContentHandlerDoFileSearch(), j_directory,
|
|
|
|
j_extensions, recursive));
|
2021-05-24 22:01:49 +02:00
|
|
|
|
2022-12-25 16:30:51 +01:00
|
|
|
std::vector<std::string> result = JStringArrayToVector(env, j_result);
|
2021-05-24 22:01:49 +02:00
|
|
|
|
2022-12-25 16:30:51 +01:00
|
|
|
env->DeleteLocalRef(j_directory);
|
|
|
|
env->DeleteLocalRef(j_extensions);
|
|
|
|
env->DeleteLocalRef(j_result);
|
2021-05-24 22:01:49 +02:00
|
|
|
|
|
|
|
return result;
|
2020-12-28 13:25:24 +01:00
|
|
|
}
|
|
|
|
|
2020-10-01 12:10:34 +04:00
|
|
|
int GetNetworkIpAddress()
|
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
|
|
|
return env->CallStaticIntMethod(IDCache::GetNetworkHelperClass(),
|
|
|
|
IDCache::GetNetworkHelperGetNetworkIpAddress());
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetNetworkPrefixLength()
|
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
|
|
|
return env->CallStaticIntMethod(IDCache::GetNetworkHelperClass(),
|
|
|
|
IDCache::GetNetworkHelperGetNetworkPrefixLength());
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetNetworkGateway()
|
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
|
|
|
return env->CallStaticIntMethod(IDCache::GetNetworkHelperClass(),
|
|
|
|
IDCache::GetNetworkHelperGetNetworkGateway());
|
|
|
|
}
|