1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
// Copyright 2018 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "jni/AndroidCommon/AndroidCommon.h"
#include <algorithm>
#include <ios>
#include <span>
#include <string>
#include <string_view>
#include <vector>
#include <jni.h>
#include "Common/Assert.h"
#include "Common/Logging/Log.h"
#include "Common/StringUtil.h"
#include "jni/AndroidCommon/IDCache.h"
std::string GetJString(JNIEnv* env, jstring jstr)
{
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);
std::string converted_string = UTF16ToUTF8(string_view);
env->ReleaseStringChars(jstr, jchars);
return converted_string;
}
jstring ToJString(JNIEnv* env, std::string_view str)
{
const std::u16string converted_string = UTF8ToUTF16(str);
return env->NewString(reinterpret_cast<const jchar*>(converted_string.data()),
converted_string.size());
}
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)
{
jstring str = reinterpret_cast<jstring>(env->GetObjectArrayElement(array, i));
result.push_back(GetJString(env, str));
env->DeleteLocalRef(str);
}
return result;
}
jobjectArray SpanToJStringArray(JNIEnv* env, std::span<const std::string_view> span)
{
return SpanToJObjectArray(env, span, IDCache::GetStringClass(), ToJString);
}
jobjectArray SpanToJStringArray(JNIEnv* env, std::span<const std::string> span)
{
return SpanToJObjectArray(env, span, IDCache::GetStringClass(), ToJString);
}
bool IsPathAndroidContent(std::string_view uri)
{
return uri.starts_with("content://");
}
std::string OpenModeToAndroid(std::string mode)
{
// The 'b' specifier is not supported by Android. Since we're on POSIX, it's fine to just skip it.
std::erase(mode, 'b');
if (mode == "r")
return "r";
else if (mode == "w")
return "wt";
else if (mode == "a")
return "wa";
else if (mode == "r+")
return "rw";
else if (mode == "w+")
return "rwt";
else if (mode == "a+")
return "rwa";
ERROR_LOG_FMT(COMMON, "OpenModeToAndroid(std::string): Unsupported open mode: {}", mode);
return "";
}
std::string OpenModeToAndroid(std::ios_base::openmode mode)
{
// The 'b' specifier is not supported by Android. Since we're on POSIX, it's fine to just skip it.
mode &= ~std::ios_base::binary;
switch (mode)
{
case std::ios_base::in:
return "r";
case std::ios_base::out:
case std::ios_base::out | std::ios_base::trunc:
return "wt";
case std::ios_base::app:
case std::ios_base::out | std::ios_base::app:
return "wa";
case std::ios_base::in | std::ios_base::out:
return "rw";
case std::ios_base::in | std::ios_base::out | std::ios_base::trunc:
return "rwt";
case std::ios_base::in | std::ios_base::app:
case std::ios_base::in | std::ios_base::out | std::ios_base::app:
return "rwa";
default:
ERROR_LOG_FMT(COMMON,
"OpenModeToAndroid(std::ios_base::openmode): Unsupported open mode: {:#x}", mode);
return "";
}
}
int OpenAndroidContent(std::string_view uri, std::string_view mode)
{
JNIEnv* env = IDCache::GetEnvForThread();
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;
}
bool DeleteAndroidContent(std::string_view uri)
{
JNIEnv* env = IDCache::GetEnvForThread();
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);
}
jlong GetAndroidContentSizeAndIsDirectory(std::string_view uri)
{
JNIEnv* env = IDCache::GetEnvForThread();
jstring j_uri = ToJString(env, uri);
jlong result = env->CallStaticLongMethod(
IDCache::GetContentHandlerClass(), IDCache::GetContentHandlerGetSizeAndIsDirectory(), j_uri);
env->DeleteLocalRef(j_uri);
return result;
}
std::string GetAndroidContentDisplayName(std::string_view uri)
{
JNIEnv* env = IDCache::GetEnvForThread();
jstring j_uri = ToJString(env, uri);
jstring j_result = reinterpret_cast<jstring>(env->CallStaticObjectMethod(
IDCache::GetContentHandlerClass(), IDCache::GetContentHandlerGetDisplayName(), j_uri));
env->DeleteLocalRef(j_uri);
if (!j_result)
return "";
std::string result = GetJString(env, j_result);
env->DeleteLocalRef(j_result);
return result;
}
std::vector<std::string> GetAndroidContentChildNames(std::string_view uri)
{
JNIEnv* env = IDCache::GetEnvForThread();
jstring j_uri = ToJString(env, uri);
jobjectArray j_result = reinterpret_cast<jobjectArray>(env->CallStaticObjectMethod(
IDCache::GetContentHandlerClass(), IDCache::GetContentHandlerGetChildNames(), j_uri, false));
std::vector<std::string> result = JStringArrayToVector(env, j_result);
env->DeleteLocalRef(j_uri);
env->DeleteLocalRef(j_result);
return result;
}
std::vector<std::string> DoFileSearchAndroidContent(std::string_view directory,
std::span<const std::string_view> extensions,
bool recursive)
{
JNIEnv* env = IDCache::GetEnvForThread();
jstring j_directory = ToJString(env, directory);
jobjectArray j_extensions = SpanToJStringArray(env, extensions);
jobjectArray j_result = reinterpret_cast<jobjectArray>(env->CallStaticObjectMethod(
IDCache::GetContentHandlerClass(), IDCache::GetContentHandlerDoFileSearch(), j_directory,
j_extensions, recursive));
std::vector<std::string> result = JStringArrayToVector(env, j_result);
env->DeleteLocalRef(j_directory);
env->DeleteLocalRef(j_extensions);
env->DeleteLocalRef(j_result);
return result;
}
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());
}
|