summaryrefslogtreecommitdiff
path: root/Source/Android/jni/AndroidCommon/AndroidCommon.h
blob: e04180ef20b2a6269b4c77a1d342aa2001aef759 (plain)
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
// Copyright 2018 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <ios>
#include <span>
#include <string>
#include <string_view>
#include <vector>

#include <jni.h>

std::string GetJString(JNIEnv* env, jstring jstr);
jstring ToJString(JNIEnv* env, std::string_view str);

std::vector<std::string> JStringArrayToVector(JNIEnv* env, jobjectArray array);
jobjectArray SpanToJStringArray(JNIEnv* env, std::span<const std::string_view> span);
jobjectArray SpanToJStringArray(JNIEnv* env, std::span<const std::string> span);

template <typename T, typename F>
jobjectArray SpanToJObjectArray(JNIEnv* env, std::span<const T> span, jclass clazz, F f)
{
  const auto span_size = static_cast<jsize>(span.size());
  jobjectArray result = env->NewObjectArray(span_size, clazz, nullptr);
  for (jsize i = 0; i < span_size; ++i)
  {
    jobject obj = f(env, span[i]);
    env->SetObjectArrayElement(result, i, obj);
    env->DeleteLocalRef(obj);
  }
  return result;
}

template <typename T, typename F>
inline jobjectArray VectorToJObjectArray(JNIEnv* env, const std::vector<T>& vector, jclass clazz,
                                         F f)
{
  return SpanToJObjectArray(env, std::span(vector), clazz, f);
}

// Returns true if the given path should be opened as Android content instead of a normal file.
bool IsPathAndroidContent(std::string_view uri);

// Turns a C/C++ style mode (e.g. "rb") into one which can be used with OpenAndroidContent.
std::string OpenModeToAndroid(std::string mode);
std::string OpenModeToAndroid(std::ios_base::openmode mode);

// Opens a given file and returns a file descriptor.
int OpenAndroidContent(std::string_view uri, std::string_view mode);

// Deletes a given file.
bool DeleteAndroidContent(std::string_view uri);
// Returns -1 if not found, -2 if directory, file size otherwise.
jlong GetAndroidContentSizeAndIsDirectory(std::string_view uri);

// An unmangled URI (one which the C++ code has not appended anything to) can't be relied on
// to contain a file name at all. If a file name is desired, this function is the most reliable
// way to get it, but the display name is not guaranteed to always actually be like a file name.
// An empty string will be returned for files which do not exist.
std::string GetAndroidContentDisplayName(std::string_view uri);

// Returns the display names of all children of a directory, non-recursively.
std::vector<std::string> GetAndroidContentChildNames(std::string_view uri);

std::vector<std::string> DoFileSearchAndroidContent(std::string_view directory,
                                                    std::span<const std::string_view> extensions,
                                                    bool recursive);

int GetNetworkIpAddress();
int GetNetworkPrefixLength();
int GetNetworkGateway();