2014-03-01 20:20:29 +01:00
|
|
|
/*
|
2022-07-14 17:58:12 +02:00
|
|
|
* Copyright 2003-2022 The Music Player Daemon Project
|
2014-03-01 20:20:29 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Context.hxx"
|
|
|
|
#include "java/Class.hxx"
|
2019-04-24 14:44:06 +02:00
|
|
|
#include "java/Exception.hxx"
|
2014-03-01 20:20:29 +01:00
|
|
|
#include "java/File.hxx"
|
2020-03-23 21:34:18 +01:00
|
|
|
#include "java/String.hxx"
|
2014-03-01 20:20:29 +01:00
|
|
|
#include "fs/AllocatedPath.hxx"
|
|
|
|
|
2020-03-23 21:34:18 +01:00
|
|
|
#include "AudioManager.hxx"
|
|
|
|
|
2022-08-18 15:12:18 +02:00
|
|
|
static jmethodID getExternalFilesDir_method,
|
|
|
|
getCacheDir_method,
|
|
|
|
getSystemService_method;
|
|
|
|
|
|
|
|
void
|
|
|
|
Context::Initialise(JNIEnv *env) noexcept
|
2021-08-24 21:58:09 +02:00
|
|
|
{
|
2022-08-18 15:12:18 +02:00
|
|
|
Java::Class cls{env, "android/content/Context"};
|
2021-08-24 21:58:09 +02:00
|
|
|
|
2022-08-18 15:12:18 +02:00
|
|
|
getExternalFilesDir_method = env->GetMethodID(cls, "getExternalFilesDir",
|
|
|
|
"(Ljava/lang/String;)Ljava/io/File;");
|
|
|
|
getCacheDir_method = env->GetMethodID(cls, "getCacheDir",
|
|
|
|
"()Ljava/io/File;");
|
|
|
|
getSystemService_method = env->GetMethodID(cls, "getSystemService",
|
|
|
|
"(Ljava/lang/String;)Ljava/lang/Object;");
|
|
|
|
}
|
2021-08-24 21:58:09 +02:00
|
|
|
|
|
|
|
AllocatedPath
|
2022-08-18 16:49:02 +02:00
|
|
|
Context::GetExternalFilesDir(JNIEnv *env, const char *type) noexcept
|
2021-08-24 21:58:09 +02:00
|
|
|
{
|
2022-10-14 22:57:08 +02:00
|
|
|
assert(type != nullptr);
|
2021-08-24 21:58:09 +02:00
|
|
|
|
2022-08-18 16:49:02 +02:00
|
|
|
jobject file = env->CallObjectMethod(Get(), getExternalFilesDir_method,
|
|
|
|
Java::String::Optional(env, type).Get());
|
2021-08-24 21:58:09 +02:00
|
|
|
if (Java::DiscardException(env) || file == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return Java::File::ToAbsolutePath(env, file);
|
|
|
|
}
|
|
|
|
|
2014-03-01 20:20:29 +01:00
|
|
|
AllocatedPath
|
2019-04-24 14:49:24 +02:00
|
|
|
Context::GetCacheDir(JNIEnv *env) const noexcept
|
2014-03-01 20:20:29 +01:00
|
|
|
{
|
|
|
|
assert(env != nullptr);
|
|
|
|
|
2022-08-18 15:12:18 +02:00
|
|
|
jobject file = env->CallObjectMethod(Get(), getCacheDir_method);
|
2019-04-24 14:44:06 +02:00
|
|
|
if (Java::DiscardException(env) || file == nullptr)
|
2018-01-17 12:17:41 +01:00
|
|
|
return nullptr;
|
2014-03-01 20:20:29 +01:00
|
|
|
|
|
|
|
return Java::File::ToAbsolutePath(env, file);
|
|
|
|
}
|
2020-03-23 21:34:18 +01:00
|
|
|
|
|
|
|
AudioManager *
|
|
|
|
Context::GetAudioManager(JNIEnv *env) noexcept
|
|
|
|
{
|
|
|
|
assert(env != nullptr);
|
|
|
|
|
|
|
|
Java::String name(env, "audio");
|
2022-08-18 15:12:18 +02:00
|
|
|
jobject am = env->CallObjectMethod(Get(), getSystemService_method, name.Get());
|
2020-03-23 21:34:18 +01:00
|
|
|
if (Java::DiscardException(env) || am == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return new AudioManager(env, am);
|
|
|
|
}
|