1
0
mirror of https://github.com/nerzhul/ownCloud-SMS-App.git synced 2026-08-18 20:03:04 +00:00

Update tools versions & prepare JNI work to reduce memory & CPU usage

This commit is contained in:
Loic Blot
2017-07-29 14:54:49 +02:00
committed by Loïc Blot
parent c52168c939
commit 47c2923d0e
14 changed files with 465 additions and 25 deletions
+56
View File
@@ -0,0 +1,56 @@
/**
* Copyright (c) 2017, Loic Blot <loic.blot@unix-experience.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "httpclient.h"
const char *HTTPClient::classPathName = "fr/unix_experience/owncloud_sms/engine/OCHttpClient";
// See https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html
JNINativeMethod HTTPClient::methods[] =
{
DECL_JNIMETHOD(getAllSmsIdsCall, "()Ljava/lang/String;")
DECL_JNIMETHOD(getLastMsgTimestamp, "()Ljava/lang/String;")
DECL_JNIMETHOD(getPushRoute, "()Ljava/lang/String;")
DECL_JNIMETHOD(getVersionCall, "()Ljava/lang/String;")
};
DECL_METHODSIZE(HTTPClient)
// APIv1 calls
#define RCALL_GET_VERSION "/index.php/apps/ocsms/get/apiversion?format=json"
#define RCALL_GET_ALL_SMS_IDS "/index.php/apps/ocsms/get/smsidlist?format=json"
#define RCALL_GET_LAST_MSG_TIMESTAMP "/index.php/apps/ocsms/get/lastmsgtime?format=json"
#define RCALL_PUSH_ROUTE "/index.php/apps/ocsms/push?format=json"
jstring HTTPClient::getVersionCall(JNIEnv *env, jobject)
{
return env->NewStringUTF(RCALL_GET_VERSION);
}
jstring HTTPClient::getAllSmsIdsCall(JNIEnv *env, jobject)
{
return env->NewStringUTF(RCALL_GET_ALL_SMS_IDS);
}
jstring HTTPClient::getLastMsgTimestamp(JNIEnv *env, jobject)
{
return env->NewStringUTF(RCALL_GET_LAST_MSG_TIMESTAMP);
}
jstring HTTPClient::getPushRoute(JNIEnv *env, jobject)
{
return env->NewStringUTF(RCALL_PUSH_ROUTE);
}
+34
View File
@@ -0,0 +1,34 @@
/**
* Copyright (c) 2017, Loic Blot <loic.blot@unix-experience.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <jni.h>
#include "macro_helpers.h"
class HTTPClient
{
public:
JNIEXPORT static jstring JNICALL getVersionCall(JNIEnv *env, jobject);
JNIEXPORT static jstring JNICALL getAllSmsIdsCall(JNIEnv *env, jobject);
JNIEXPORT static jstring JNICALL getLastMsgTimestamp(JNIEnv *env, jobject);
JNIEXPORT static jstring JNICALL getPushRoute(JNIEnv *env, jobject);
DECL_JNICLASSATTRS
};
+29
View File
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2017, Loic Blot <loic.blot@unix-experience.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#define DECL_JNIMETHOD(name, type) \
{#name, type, (void*) &name },
#define DECL_METHODSIZE(T) \
int T::methods_size = sizeof(T::methods) / sizeof(T::methods[0]);
#define DECL_JNICLASSATTRS \
static const char *classPathName; \
static JNINativeMethod methods[]; \
static int methods_size;
+76
View File
@@ -0,0 +1,76 @@
/**
* Copyright (c) 2014-2017, Loic Blot <loic.blot@unix-experience.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <jni.h>
#include <android/log.h>
#include "httpclient.h"
#include "smsbuffer.h"
#define LOG_TAG "nativesms.cpp"
/*
* Register several native methods for one class.
*/
static int registerNativeMethods(JNIEnv* env, const char* className,
JNINativeMethod* gMethods, int numMethods)
{
jclass clazz;
clazz = env->FindClass(className);
__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "Registering class '%s'", className);
if (clazz == NULL) {
__android_log_print(ANDROID_LOG_FATAL, LOG_TAG, "Native registration unable to find class %s", className);
return JNI_FALSE;
}
if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "RegisterNatives failed for %s", className);
return JNI_FALSE;
}
return JNI_TRUE;
}
static int registerNatives(JNIEnv* env)
{
if (!registerNativeMethods(env,
HTTPClient::classPathName,
HTTPClient::methods,
HTTPClient::methods_size)) {
return JNI_FALSE;
}
if (!registerNativeMethods(env,
SmsBuffer::classPathName,
SmsBuffer::methods,
SmsBuffer::methods_size)) {
return JNI_FALSE;
}
return JNI_TRUE;
}
jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "NativeSMS library loading...");
JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
return -1;
}
registerNatives(env);
__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "NativeSMS library loaded.");
return JNI_VERSION_1_6;
}
+94
View File
@@ -0,0 +1,94 @@
/**
* Copyright (c) 2017, Loic Blot <loic.blot@unix-experience.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <android/log.h>
#include "smsbuffer.h"
#define LOG_TAG "SmsBuffer"
const char *SmsBuffer::classPathName = "fr/unix_experience/owncloud_sms/jni/SmsBuffer";
// See https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html
JNINativeMethod SmsBuffer::methods[] =
{
DECL_JNIMETHOD(createNativeObject, "()J")
DECL_JNIMETHOD(deleteNativeObject, "(J)V")
DECL_JNIMETHOD(push, "(JI)V")
DECL_JNIMETHOD(print, "(J)V")
};
DECL_METHODSIZE(SmsBuffer)
jlong SmsBuffer::createNativeObject(JNIEnv *env, jobject self)
{
return reinterpret_cast<jlong>(new SmsBuffer());
}
void SmsBuffer::deleteNativeObject(JNIEnv *env, jobject self, jlong ptr)
{
__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "deleteSmsBuffer");
delete reinterpret_cast<SmsBuffer *>(ptr);
}
SmsBuffer::SmsBuffer()
{
}
void SmsBuffer::reset_buffer()
{
m_buffer.clear();
m_buffer_empty = true;
}
void SmsBuffer::push(JNIEnv *env, jobject self, jlong ptr, jint mailbox_id)
{
SmsBuffer *me = reinterpret_cast<SmsBuffer *>(ptr);
if (!me) {
__android_log_print(ANDROID_LOG_FATAL, LOG_TAG, "It's not a SmsBuffer!");
return;
}
me->_push(mailbox_id);
}
void SmsBuffer::_push(int mailbox_id)
{
// If buffer is not empty, we are joining messages
if (!m_buffer_empty) {
m_buffer << ",";
}
// Else, we are starting array
else {
m_buffer << "[";
m_buffer_empty = true;
}
m_buffer << "{\"mbox\": " << mailbox_id << "}";
}
void SmsBuffer::print(JNIEnv *env, jobject self, jlong ptr)
{
SmsBuffer *me = reinterpret_cast<SmsBuffer *>(ptr);
if (!me) {
__android_log_print(ANDROID_LOG_FATAL, LOG_TAG, "It's not a SmsBuffer!");
return;
}
me->_print();
}
void SmsBuffer::_print()
{
__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "SmsBuffer content: '%s'", m_buffer.str().c_str());
}
+44
View File
@@ -0,0 +1,44 @@
/**
* Copyright (c) 2017, Loic Blot <loic.blot@unix-experience.fr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <jni.h>
#include <sstream>
#include "macro_helpers.h"
class SmsBuffer
{
public:
SmsBuffer();
JNIEXPORT static jlong JNICALL createNativeObject(JNIEnv *env, jobject self);
JNIEXPORT static void JNICALL deleteNativeObject(JNIEnv *env, jobject self, jlong ptr);
JNIEXPORT static void JNICALL push(JNIEnv *env, jobject self, jlong ptr, jint mailbox_id);
void _push(int mailbox_id);
JNIEXPORT static void JNICALL print(JNIEnv *env, jobject self, jlong ptr);
void _print();
DECL_JNICLASSATTRS
private:
void reset_buffer();
std::stringstream m_buffer;
bool m_buffer_empty{true};
};