Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Share files using FileProvider #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,10 @@ CJNIResources CJNIContext::getResources()
return call_method<jhobject>(m_context,
"getResources", "()Landroid/content/res/Resources;");
}

void CJNIContext::grantUriPermission(const std::string& package, const CJNIURI& uri, int flags)
{
call_method<void>(m_context,
"grantUriPermission", "(Ljava/lang/String;Landroid/net/Uri;I)V",
jcast<jhstring>(package), uri.get_raw(), flags);
}
2 changes: 2 additions & 0 deletions src/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class CJNIContentResolver;
class CJNIWindow;
class CJNIResourcesTheme;
class CJNIResources;
class CJNIURI;

class CJNIContext
{
Expand Down Expand Up @@ -70,6 +71,7 @@ class CJNIContext
static CJNIWindow getWindow();
static CJNIResourcesTheme getTheme();
static CJNIResources getResources();
static void grantUriPermission(const std::string& package, const CJNIURI& uri, int flags);

protected:
CJNIContext(const ANativeActivity *nativeActivity);
Expand Down
11 changes: 11 additions & 0 deletions src/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@

using namespace jni;

CJNIFile::CJNIFile(const std::string& pathname) : CJNIBase("java/io/File")
{
m_object = new_object(GetClassName(), "<init>", "(Ljava/lang/String;)V", jcast<jhstring>(pathname));
m_object.setGlobal();
}

bool CJNIFile::exists()
{
return call_method<jboolean>(m_object, "exists", "()Z");
}

std::string CJNIFile::getAbsolutePath()
{
return jcast<std::string>(call_method<jhstring>(m_object,
Expand Down
2 changes: 2 additions & 0 deletions src/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ class CJNIFile : public CJNIBase
public:
CJNIFile();
CJNIFile(const jni::jhobject &file) : CJNIBase(file){};
CJNIFile(const std::string& pathname);
~CJNIFile(){};

bool exists();
std::string getAbsolutePath();
int64_t getUsableSpace();
};
24 changes: 24 additions & 0 deletions src/FileProvider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2024 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/

#include "FileProvider.h"

#include "jutils-details.hpp"

using namespace jni;

const std::string CJNIFileProvider::m_classname = "androidx/core/content/FileProvider";

const CJNIURI CJNIFileProvider::getUriForFile(const CJNIContext& context, const std::string& authority, const CJNIFile& file)
{
const jhclass clazz = CJNIContext::getClassLoader().loadClass(m_classname);

return call_static_method<jhobject>(clazz,
"getUriForFile", "(Landroid/content/Context;Ljava/lang/String;Ljava/io/File;)Landroid/net/Uri;",
context.get_raw(), jcast<jhstring>(authority), file.get_raw());
};
22 changes: 22 additions & 0 deletions src/FileProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
/*
* Copyright (C) 2024 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/

#include "JNIBase.h"
#include "Context.h"
#include "File.h"
#include "URI.h"

class CJNIFileProvider : public CJNIBase
{
public:
static const CJNIURI getUriForFile(const CJNIContext& context, const std::string& authority, const CJNIFile& file);

private:
static const std::string m_classname;
};
4 changes: 4 additions & 0 deletions src/Intent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ std::string CJNIIntent::ACTION_SCREEN_OFF;
std::string CJNIIntent::ACTION_SEARCH;
std::string CJNIIntent::ACTION_VIEW;
std::string CJNIIntent::EXTRA_KEY_EVENT;
int CJNIIntent::FLAG_GRANT_READ_URI_PERMISSION;
int CJNIIntent::FLAG_GRANT_WRITE_URI_PERMISSION;

static std::string s_className = "android/content/Intent";

Expand All @@ -67,6 +69,8 @@ void CJNIIntent::PopulateStaticFields()
ACTION_SEARCH = jcast<std::string>(get_static_field<jhstring>(clazz,"ACTION_SEARCH"));
ACTION_VIEW = jcast<std::string>(get_static_field<jhstring>(clazz,"ACTION_VIEW"));
EXTRA_KEY_EVENT = jcast<std::string>(get_static_field<jhstring>(clazz,"EXTRA_KEY_EVENT"));
FLAG_GRANT_READ_URI_PERMISSION = (get_static_field<int>(clazz, "FLAG_GRANT_READ_URI_PERMISSION"));
FLAG_GRANT_WRITE_URI_PERMISSION = (get_static_field<int>(clazz, "FLAG_GRANT_WRITE_URI_PERMISSION"));
}

CJNIIntent::CJNIIntent(const std::string &action)
Expand Down
2 changes: 2 additions & 0 deletions src/Intent.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,6 @@ class CJNIIntent : public CJNIBase
static std::string ACTION_SEARCH;
static std::string ACTION_VIEW;
static std::string EXTRA_KEY_EVENT;
static int FLAG_GRANT_READ_URI_PERMISSION;
static int FLAG_GRANT_WRITE_URI_PERMISSION;
};