2020-07-06 15:02:01 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-07-06 15:02:01 +00:00
|
|
|
|
|
|
|
#include "jni/AndroidCommon/AndroidCommon.h"
|
|
|
|
|
2021-10-13 15:39:09 +00:00
|
|
|
#include <algorithm>
|
2020-11-04 19:59:39 +00:00
|
|
|
#include <ios>
|
2020-07-06 15:02:01 +00:00
|
|
|
#include <string>
|
2020-06-28 17:16:23 +00:00
|
|
|
#include <string_view>
|
2020-07-06 15:02:01 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <jni.h>
|
|
|
|
|
2020-11-04 19:59:39 +00:00
|
|
|
#include "Common/Assert.h"
|
2020-06-28 17:16:23 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2020-06-26 15:52:31 +00:00
|
|
|
#include "jni/AndroidCommon/IDCache.h"
|
2020-06-28 17:16:23 +00:00
|
|
|
|
2020-07-06 15:02:01 +00:00
|
|
|
std::string GetJString(JNIEnv* env, jstring jstr)
|
|
|
|
{
|
2020-06-28 17:16:23 +00: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 21:28:06 +00:00
|
|
|
std::string converted_string = UTF16ToUTF8(string_view);
|
2020-06-28 17:16:23 +00:00
|
|
|
env->ReleaseStringChars(jstr, jchars);
|
|
|
|
return converted_string;
|
2020-07-06 15:02:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 18:24:53 +00:00
|
|
|
jstring ToJString(JNIEnv* env, std::string_view str)
|
2020-07-06 15:02:01 +00:00
|
|
|
{
|
2020-06-28 17:16:23 +00:00
|
|
|
const std::u16string converted_string = UTF8ToUTF16(str);
|
|
|
|
return env->NewString(reinterpret_cast<const jchar*>(converted_string.data()),
|
|
|
|
converted_string.size());
|
2020-07-06 15:02:01 +00: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 20:01:49 +00:00
|
|
|
{
|
|
|
|
jstring str = reinterpret_cast<jstring>(env->GetObjectArrayElement(array, i));
|
|
|
|
result.push_back(GetJString(env, str));
|
|
|
|
env->DeleteLocalRef(str);
|
|
|
|
}
|
2020-07-06 15:02:01 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2020-06-26 15:52:31 +00:00
|
|
|
|
2022-12-27 21:28:06 +00:00
|
|
|
jobjectArray VectorToJStringArray(JNIEnv* env, const std::vector<std::string>& vector)
|
2020-12-28 12:25:24 +00:00
|
|
|
{
|
2022-12-27 21:28:06 +00:00
|
|
|
return VectorToJObjectArray(env, vector, IDCache::GetStringClass(), ToJString);
|
2020-12-28 12:25:24 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 18:24:53 +00:00
|
|
|
bool IsPathAndroidContent(std::string_view uri)
|
2020-06-26 15:52:31 +00:00
|
|
|
{
|
2023-01-24 19:25:49 +00:00
|
|
|
return uri.starts_with("content://");
|
2020-11-04 19:59:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string OpenModeToAndroid(std::string mode)
|
|
|
|
{
|
2021-05-24 20:01:49 +00:00
|
|
|
// The 'b' specifier is not supported by Android. Since we're on POSIX, it's fine to just skip it.
|
2021-10-13 15:39:09 +00:00
|
|
|
mode.erase(std::remove(mode.begin(), mode.end(), 'b'));
|
2020-06-26 15:52:31 +00:00
|
|
|
|
2020-11-04 19:59:39 +00: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 15:52:31 +00:00
|
|
|
|
2020-11-04 19:59:39 +00:00
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2020-11-04 19:59:39 +00: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 20:01:49 +00:00
|
|
|
// The 'b' specifier is not supported by Android. Since we're on POSIX, it's fine to just skip it.
|
2020-11-04 19:59:39 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-01-30 18:24:53 +00:00
|
|
|
int OpenAndroidContent(std::string_view uri, std::string_view mode)
|
2020-06-26 15:52:31 +00:00
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
2022-12-25 15:30:51 +00: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 15:52:31 +00:00
|
|
|
}
|
2020-06-27 09:04:48 +00:00
|
|
|
|
2023-01-30 18:24:53 +00:00
|
|
|
bool DeleteAndroidContent(std::string_view uri)
|
2020-06-27 09:04:48 +00:00
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
2022-12-25 15:30:51 +00: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 09:04:48 +00:00
|
|
|
}
|
2020-10-01 08:10:34 +00:00
|
|
|
|
2023-01-30 18:24:53 +00:00
|
|
|
jlong GetAndroidContentSizeAndIsDirectory(std::string_view uri)
|
2020-11-05 18:47:23 +00:00
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
2022-12-25 15:30:51 +00: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 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 18:24:53 +00:00
|
|
|
std::string GetAndroidContentDisplayName(std::string_view uri)
|
2020-11-08 14:39:17 +00:00
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
2021-05-24 20:01:49 +00:00
|
|
|
|
2022-12-25 15:30:51 +00:00
|
|
|
jstring j_uri = ToJString(env, uri);
|
2021-05-24 20:01:49 +00:00
|
|
|
|
2022-12-25 15:30:51 +00: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 20:01:49 +00:00
|
|
|
return "";
|
|
|
|
|
2022-12-25 15:30:51 +00:00
|
|
|
std::string result = GetJString(env, j_result);
|
2021-05-24 20:01:49 +00:00
|
|
|
|
2022-12-25 15:30:51 +00:00
|
|
|
env->DeleteLocalRef(j_result);
|
2021-05-24 20:01:49 +00:00
|
|
|
|
|
|
|
return result;
|
2020-11-08 14:39:17 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 18:24:53 +00:00
|
|
|
std::vector<std::string> GetAndroidContentChildNames(std::string_view uri)
|
2020-11-08 15:57:49 +00:00
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
2021-05-24 20:01:49 +00:00
|
|
|
|
2022-12-25 15:30:51 +00:00
|
|
|
jstring j_uri = ToJString(env, uri);
|
2021-05-24 20:01:49 +00:00
|
|
|
|
2022-12-25 15:30:51 +00:00
|
|
|
jobjectArray j_result = reinterpret_cast<jobjectArray>(env->CallStaticObjectMethod(
|
|
|
|
IDCache::GetContentHandlerClass(), IDCache::GetContentHandlerGetChildNames(), j_uri, false));
|
2021-05-24 20:01:49 +00:00
|
|
|
|
2022-12-25 15:30:51 +00:00
|
|
|
std::vector<std::string> result = JStringArrayToVector(env, j_result);
|
|
|
|
|
|
|
|
env->DeleteLocalRef(j_uri);
|
|
|
|
env->DeleteLocalRef(j_result);
|
2021-05-24 20:01:49 +00:00
|
|
|
|
|
|
|
return result;
|
2020-11-08 15:57:49 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 18:24:53 +00:00
|
|
|
std::vector<std::string> DoFileSearchAndroidContent(std::string_view directory,
|
2020-12-28 12:25:24 +00:00
|
|
|
const std::vector<std::string>& extensions,
|
|
|
|
bool recursive)
|
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
2021-05-24 20:01:49 +00:00
|
|
|
|
2022-12-25 15:30:51 +00: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 20:01:49 +00:00
|
|
|
|
2022-12-25 15:30:51 +00:00
|
|
|
std::vector<std::string> result = JStringArrayToVector(env, j_result);
|
2021-05-24 20:01:49 +00:00
|
|
|
|
2022-12-25 15:30:51 +00:00
|
|
|
env->DeleteLocalRef(j_directory);
|
|
|
|
env->DeleteLocalRef(j_extensions);
|
|
|
|
env->DeleteLocalRef(j_result);
|
2021-05-24 20:01:49 +00:00
|
|
|
|
|
|
|
return result;
|
2020-12-28 12:25:24 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 08:10:34 +00: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());
|
|
|
|
}
|