2020-07-06 15:02:01 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "jni/AndroidCommon/AndroidCommon.h"
|
|
|
|
|
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);
|
|
|
|
const std::string converted_string = UTF16ToUTF8(string_view);
|
|
|
|
env->ReleaseStringChars(jstr, jchars);
|
|
|
|
return converted_string;
|
2020-07-06 15:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jstring ToJString(JNIEnv* env, const std::string& str)
|
|
|
|
{
|
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)
|
|
|
|
result.push_back(GetJString(env, (jstring)env->GetObjectArrayElement(array, i)));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2020-06-26 15:52:31 +00:00
|
|
|
|
2020-11-04 19:59:39 +00:00
|
|
|
bool IsPathAndroidContent(const std::string& uri)
|
2020-06-26 15:52:31 +00:00
|
|
|
{
|
2020-11-04 19:59:39 +00:00
|
|
|
return StringBeginsWith(uri, "content://");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string OpenModeToAndroid(std::string mode)
|
|
|
|
{
|
|
|
|
// The 'b' specifier is not supported. Since we're on POSIX, it's fine to just skip it.
|
|
|
|
if (!mode.empty() && mode.back() == 'b')
|
|
|
|
mode.pop_back();
|
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';
|
|
|
|
|
|
|
|
// The 'b' specifier is not supported. Since we're on POSIX, it's fine to just skip it.
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-06-26 15:52:31 +00:00
|
|
|
int OpenAndroidContent(const std::string& uri, const std::string& mode)
|
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
2020-11-04 19:59:39 +00:00
|
|
|
return env->CallStaticIntMethod(IDCache::GetContentHandlerClass(),
|
|
|
|
IDCache::GetContentHandlerOpenFd(), ToJString(env, uri),
|
|
|
|
ToJString(env, mode));
|
2020-06-26 15:52:31 +00:00
|
|
|
}
|
2020-06-27 09:04:48 +00:00
|
|
|
|
|
|
|
bool DeleteAndroidContent(const std::string& uri)
|
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
|
|
|
return env->CallStaticBooleanMethod(IDCache::GetContentHandlerClass(),
|
|
|
|
IDCache::GetContentHandlerDelete(), ToJString(env, uri));
|
|
|
|
}
|
2020-10-01 08:10:34 +00:00
|
|
|
|
2020-11-05 18:47:23 +00:00
|
|
|
jlong GetAndroidContentSizeAndIsDirectory(const std::string& uri)
|
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
|
|
|
return env->CallStaticLongMethod(IDCache::GetContentHandlerClass(),
|
|
|
|
IDCache::GetContentHandlerGetSizeAndIsDirectory(),
|
|
|
|
ToJString(env, uri));
|
|
|
|
}
|
|
|
|
|
2020-11-08 14:39:17 +00:00
|
|
|
std::string GetAndroidContentDisplayName(const std::string& uri)
|
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
|
|
|
jobject display_name =
|
|
|
|
env->CallStaticObjectMethod(IDCache::GetContentHandlerClass(),
|
|
|
|
IDCache::GetContentHandlerGetDisplayName(), ToJString(env, uri));
|
|
|
|
return display_name ? GetJString(env, reinterpret_cast<jstring>(display_name)) : "";
|
|
|
|
}
|
|
|
|
|
2020-11-08 15:57:49 +00:00
|
|
|
std::vector<std::string> GetAndroidContentChildNames(const std::string& uri)
|
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
|
|
|
jobject children =
|
|
|
|
env->CallStaticObjectMethod(IDCache::GetContentHandlerClass(),
|
|
|
|
IDCache::GetContentHandlerGetChildNames(), ToJString(env, uri));
|
|
|
|
return JStringArrayToVector(env, reinterpret_cast<jobjectArray>(children));
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|