diff options
author | Chris "Koying" Browet <cbro@semperpax.com> | 2018-01-07 13:25:30 +0100 |
---|---|---|
committer | Chris "Koying" Browet <cbro@semperpax.com> | 2018-01-09 19:40:27 +0100 |
commit | b1425bb2b6606f189c0db3bd9d3054434371d7fd (patch) | |
tree | 476de89ee686fc46e791fb41cdcc397d5d80cd9d | |
parent | d139c83822cd1eec51b8db4cff135a1897337e5c (diff) |
CHG: [droid] use jni to read files
-rw-r--r-- | cmake/scripts/android/Install.cmake | 1 | ||||
-rw-r--r-- | tools/android/packaging/xbmc/src/XBMCFile.java.in | 99 | ||||
-rw-r--r-- | tools/android/packaging/xbmc/src/XBMCJsonRPC.java.in | 22 | ||||
-rw-r--r-- | tools/android/packaging/xbmc/src/content/XBMCImageContentProvider.java.in | 40 | ||||
-rw-r--r-- | xbmc/platform/android/activity/CMakeLists.txt | 2 | ||||
-rw-r--r-- | xbmc/platform/android/activity/JNIXBMCFile.cpp | 134 | ||||
-rw-r--r-- | xbmc/platform/android/activity/JNIXBMCFile.h | 50 | ||||
-rw-r--r-- | xbmc/platform/android/activity/android_main.cpp | 2 |
8 files changed, 313 insertions, 37 deletions
diff --git a/cmake/scripts/android/Install.cmake b/cmake/scripts/android/Install.cmake index a965b1ab75..c70fd92e8e 100644 --- a/cmake/scripts/android/Install.cmake +++ b/cmake/scripts/android/Install.cmake @@ -68,6 +68,7 @@ set(package_files strings.xml src/XBMCSettingsContentObserver.java src/XBMCProperties.java src/XBMCVideoView.java + src/XBMCFile.java src/channels/SyncChannelJobService.java src/channels/SyncProgramsJobService.java src/channels/model/XBMCDatabase.java diff --git a/tools/android/packaging/xbmc/src/XBMCFile.java.in b/tools/android/packaging/xbmc/src/XBMCFile.java.in new file mode 100644 index 0000000000..b99fb92773 --- /dev/null +++ b/tools/android/packaging/xbmc/src/XBMCFile.java.in @@ -0,0 +1,99 @@ +package @APP_PACKAGE@; + +import android.util.Log; + +/** + * Created by koyin on 07/01/2018. + */ + +public class XBMCFile +{ + native boolean _open(String path); + native void _close(); + native byte[] _read(); + native boolean _eof(); + + private static String TAG = "@APP_NAME@file"; + + public XBMCFile() + { + + } + + public boolean Open(String path) + { + try + { + return _open(path); + } + catch (Exception e) + { + e.printStackTrace(); + Log.e(TAG, "Open: Exception"); + return false; + } + catch (UnsatisfiedLinkError e) + { + Log.e(TAG, "Open: Not available"); + return false; + } + } + + public void Close() + { + try + { + _close(); + } + catch (Exception e) + { + e.printStackTrace(); + Log.e(TAG, "Close: Exception"); + return; + } + catch (UnsatisfiedLinkError e) + { + Log.e(TAG, "Close: Not available"); + return; + } + } + + public byte[] Read() + { + try + { + return _read(); + } + catch (Exception e) + { + e.printStackTrace(); + Log.e(TAG, "Read: Exception"); + return new byte[0]; + } + catch (UnsatisfiedLinkError e) + { + Log.e(TAG, "Read: Not available"); + return new byte[0]; + } + } + + public boolean Eof() + { + try + { + return _eof(); + } + catch (Exception e) + { + e.printStackTrace(); + Log.e(TAG, "Eof: Exception"); + return false; + } + catch (UnsatisfiedLinkError e) + { + Log.e(TAG, "Eof: Not available"); + return false; + } + } + +} diff --git a/tools/android/packaging/xbmc/src/XBMCJsonRPC.java.in b/tools/android/packaging/xbmc/src/XBMCJsonRPC.java.in index 35c61465d9..3268eebff6 100644 --- a/tools/android/packaging/xbmc/src/XBMCJsonRPC.java.in +++ b/tools/android/packaging/xbmc/src/XBMCJsonRPC.java.in @@ -147,6 +147,11 @@ public class XBMCJsonRPC e.printStackTrace(); return null; } + catch (UnsatisfiedLinkError e) + { + Log.e(TAG, "_requestJSON: Not available"); + return null; + } } public JsonObject request_object(String jsonRequest) @@ -222,22 +227,7 @@ public class XBMCJsonRPC public String getDownloadUrl(String src) { - try - { - JsonObject req = request_object("{\"jsonrpc\": \"2.0\", \"method\": \"Files.PrepareDownload\", \"params\": { \"path\": \"" - + src + "\"}, \"id\": \"1\"}"); - if (req == null || !req.has("result")) - return null; - - JsonObject result = req.getAsJsonObject("result"); - String surl = result.getAsJsonObject("details").get("path").getAsString(); - - return (m_xbmc_web_url + "/" + surl); - } catch (Exception e) - { - e.printStackTrace(); - return ""; - } + return src; } public boolean Ping() diff --git a/tools/android/packaging/xbmc/src/content/XBMCImageContentProvider.java.in b/tools/android/packaging/xbmc/src/content/XBMCImageContentProvider.java.in index aef96a09b1..37f4303269 100644 --- a/tools/android/packaging/xbmc/src/content/XBMCImageContentProvider.java.in +++ b/tools/android/packaging/xbmc/src/content/XBMCImageContentProvider.java.in @@ -12,6 +12,7 @@ import android.net.Uri; import android.os.ParcelFileDescriptor; import android.util.Log; +import @APP_PACKAGE@.XBMCFile; import @APP_PACKAGE@.XBMCProperties; /** @@ -76,20 +77,11 @@ public class XBMCImageContentProvider extends XBMCContentProvider String decodedUrl = uri.getFragment(); // Log.d(TAG, " decodedUrl: " + decodedUrl); if (decodedUrl == null) - { return null; - } - pipe = ParcelFileDescriptor.createPipe(); - URL url = new URL(decodedUrl); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - String auth = XBMCProperties.getJsonAuthorization(); - if (!auth.isEmpty()) - connection.setRequestProperty("Authorization", auth); - connection.setDoInput(true); - connection.connect(); + pipe = ParcelFileDescriptor.createPipe(); - new TransferThread(connection.getInputStream(), + new TransferThread(decodedUrl, new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1])).start(); } catch (IOException e) @@ -136,33 +128,39 @@ public class XBMCImageContentProvider extends XBMCContentProvider static class TransferThread extends Thread { - InputStream in; + String path; OutputStream out; - TransferThread(InputStream in, OutputStream out) + TransferThread(String path, OutputStream out) { - this.in = in; + this.path = path; this.out = out; } @Override public void run() { - byte[] buf = new byte[8192]; - int len; - try { - while ((len = in.read(buf)) >= 0) + XBMCFile in = new XBMCFile(); + if (!in.Open(path)) + { + out.flush(); + out.close(); + return; + } + + while (!in.Eof()) { - out.write(buf, 0, len); + byte[] buf = in.Read(); + out.write(buf, 0, buf.length); } - in.close(); + in.Close(); out.flush(); out.close(); } - catch (IOException e) + catch (Exception e) { Log.e(getClass().getSimpleName(), "Exception transferring file", e); } diff --git a/xbmc/platform/android/activity/CMakeLists.txt b/xbmc/platform/android/activity/CMakeLists.txt index 8afce0c590..9852b93b6d 100644 --- a/xbmc/platform/android/activity/CMakeLists.txt +++ b/xbmc/platform/android/activity/CMakeLists.txt @@ -15,6 +15,7 @@ set(SOURCES android_main.cpp JNIXBMCNsdManagerRegistrationListener.cpp JNIXBMCNsdManagerResolveListener.cpp JNIXBMCJsonHandler.cpp + JNIXBMCFile.cpp ${NDKROOT}/sources/android/native_app_glue/android_native_app_glue.c ${NDKROOT}/sources/android/cpufeatures/cpu-features.c) @@ -37,6 +38,7 @@ set(HEADERS AndroidExtra.h JNIXBMCNsdManagerRegistrationListener.h JNIXBMCNsdManagerResolveListener.h JNIXBMCJsonHandler.h + JNIXBMCFile.h XBMCApp.h) core_add_library(platform_android_activity) diff --git a/xbmc/platform/android/activity/JNIXBMCFile.cpp b/xbmc/platform/android/activity/JNIXBMCFile.cpp new file mode 100644 index 0000000000..7a37abc24a --- /dev/null +++ b/xbmc/platform/android/activity/JNIXBMCFile.cpp @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2018 Christian Browet + * http://xbmc.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, 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 XBMC; see the file COPYING. If not, see + * <http://www.gnu.org/licenses/>. + * + */ + +#include "JNIXBMCFile.h" +#include <androidjni/jutils-details.hpp> + +#include "CompileInfo.h" + +#include "utils/log.h" +#include "utils/FileUtils.h" + +#define BUFFSIZE 8192 + +using namespace jni; + +static std::string s_className = std::string(CCompileInfo::GetClass()) + "/XBMCFile"; + +CJNIXBMCFile::CJNIXBMCFile() + : CJNIBase() + , m_eof(true) +{ +} + +void CJNIXBMCFile::RegisterNatives(JNIEnv *env) +{ + jclass cClass = env->FindClass(s_className.c_str()); + if(cClass) + { + JNINativeMethod methods[] = + { + {"_open", "(Ljava/lang/String;)Z", (void*)&CJNIXBMCFile::_open}, + {"_close", "()V", (void*)&CJNIXBMCFile::_close}, + {"_read", "()[B", (void*)&CJNIXBMCFile::_read}, + {"_eof", "()Z", (void*)&CJNIXBMCFile::_eof}, + }; + + env->RegisterNatives(cClass, methods, sizeof(methods)/sizeof(methods[0])); + } +} + +jboolean CJNIXBMCFile::_open(JNIEnv *env, jobject thiz, jstring path) +{ + std::string strPath = jcast<std::string>(jhstring::fromJNI(path)); + + if (find_instance(thiz)) + return false; + + if (!XFILE::CFile::Exists(strPath)) + return false; + + CJNIXBMCFile* file = new CJNIXBMCFile(); + file->m_file.reset(new XFILE::CFile()); + bool ret = file->m_file->Open(strPath); + if (!ret) + { + delete file; + return false; + } + + jhobject jo = jhobject::fromJNI(thiz); + jo.setGlobal(); + add_instance(jo, file); + file->m_eof = false; + return true; +} + +void CJNIXBMCFile::_close(JNIEnv *env, jobject thiz) +{ + CJNIXBMCFile *inst = find_instance(thiz); + if (inst) + { + inst->m_file->Close(); + remove_instance(inst); + delete inst; + } +} + +jbyteArray CJNIXBMCFile::_read(JNIEnv *env, jobject thiz) +{ + ssize_t sz = 0; + char buffer[BUFFSIZE]; + + CJNIXBMCFile *inst = find_instance(thiz); + if (inst && inst->m_file) + { + sz = inst->m_file->Read((void*)buffer, BUFFSIZE); + if (sz <= 0) + { + inst->m_eof = true; + sz = 0; + } + } + + jbyteArray jba = NULL; + char* pArray; + jba = env->NewByteArray(sz); + if ((pArray = (char*)env->GetPrimitiveArrayCritical(jba, NULL))) + { + memcpy(pArray, buffer, sz); + env->ReleasePrimitiveArrayCritical(jba, pArray, 0); + } + + return jba; +} + +jboolean CJNIXBMCFile::_eof(JNIEnv *env, jobject thiz) +{ + CJNIXBMCFile *inst = find_instance(thiz); + if (inst) + return inst->m_eof; + + return true; +} + + + + diff --git a/xbmc/platform/android/activity/JNIXBMCFile.h b/xbmc/platform/android/activity/JNIXBMCFile.h new file mode 100644 index 0000000000..3423c32961 --- /dev/null +++ b/xbmc/platform/android/activity/JNIXBMCFile.h @@ -0,0 +1,50 @@ +#pragma once +/* + * Copyright (C) 2018 Christian Browet + * http://xbmc.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, 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 XBMC; see the file COPYING. If not, see + * <http://www.gnu.org/licenses/>. + * + */ + +#include <androidjni/JNIBase.h> + +#include "filesystem/File.h" + +#include <memory> + +namespace jni +{ + + class CJNIXBMCFile : public CJNIBase, public CJNIInterfaceImplem<CJNIXBMCFile> + { + public: + CJNIXBMCFile(); + CJNIXBMCFile(const jni::jhobject &object) : CJNIBase(object) {} + virtual ~CJNIXBMCFile() {} + + static void RegisterNatives(JNIEnv* env); + + protected: + bool m_eof; + std::unique_ptr<XFILE::CFile> m_file; + + static jboolean _open(JNIEnv* env, jobject thiz, jstring path); + static void _close(JNIEnv* env, jobject thiz); + static jbyteArray _read(JNIEnv* env, jobject thiz); + static jboolean _eof(JNIEnv* env, jobject thiz); + }; + +} diff --git a/xbmc/platform/android/activity/android_main.cpp b/xbmc/platform/android/activity/android_main.cpp index 8703f56be2..83f68f9ab8 100644 --- a/xbmc/platform/android/activity/android_main.cpp +++ b/xbmc/platform/android/activity/android_main.cpp @@ -38,6 +38,7 @@ #include "platform/android/activity/JNIXBMCNsdManagerRegistrationListener.h" #include "platform/android/activity/JNIXBMCNsdManagerResolveListener.h" #include "platform/android/activity/JNIXBMCJsonHandler.h" +#include "platform/android/activity/JNIXBMCFile.h" #include "utils/StringUtils.h" #include "XBMCApp.h" @@ -157,6 +158,7 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) jni::CJNIXBMCNsdManagerResolveListener::RegisterNatives(env); jni::CJNIXBMCMediaSession::RegisterNatives(env); jni::CJNIXBMCJsonHandler::RegisterNatives(env); + jni::CJNIXBMCFile::RegisterNatives(env); jclass cMain = env->FindClass(mainClass.c_str()); if(cMain) |