android: Context: add GetAudioManager

This commit is contained in:
Thomas Guillem 2020-03-23 21:34:18 +01:00 committed by Max Kellermann
parent 200258c7c3
commit 5619fd0bba
2 changed files with 25 additions and 0 deletions

View File

@ -20,8 +20,11 @@
#include "Context.hxx"
#include "java/Class.hxx"
#include "java/File.hxx"
#include "java/String.hxx"
#include "fs/AllocatedPath.hxx"
#include "AudioManager.hxx"
AllocatedPath
Context::GetCacheDir(JNIEnv *env) const noexcept
{
@ -40,3 +43,21 @@ Context::GetCacheDir(JNIEnv *env) const noexcept
return Java::File::ToAbsolutePath(env, file);
}
AudioManager *
Context::GetAudioManager(JNIEnv *env) noexcept
{
assert(env != nullptr);
Java::Class cls(env, env->GetObjectClass(Get()));
jmethodID method = env->GetMethodID(cls, "getSystemService",
"(Ljava/lang/String;)Ljava/lang/Object;");
assert(method);
Java::String name(env, "audio");
jobject am = env->CallObjectMethod(Get(), method, name.Get());
if (Java::DiscardException(env) || am == nullptr)
return nullptr;
return new AudioManager(env, am);
}

View File

@ -23,6 +23,7 @@
#include "java/Object.hxx"
class AllocatedPath;
class AudioManager;
class Context : public Java::GlobalObject {
public:
@ -31,6 +32,9 @@ public:
gcc_pure
AllocatedPath GetCacheDir(JNIEnv *env) const noexcept;
gcc_pure
AudioManager *GetAudioManager(JNIEnv *env) noexcept;
};
#endif