mirror of
https://github.com/nerzhul/ownCloud-SMS-App.git
synced 2026-08-17 11:23:04 +00:00
SmsBuffer is now working, replace json objects with the SmsBuffer on sync
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* 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 "json.h"
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
namespace json {
|
||||
|
||||
std::string escape_string(const char *str)
|
||||
{
|
||||
std::string result;
|
||||
// Create a sufficient buffer to escape all chars
|
||||
result.reserve(strlen(str) * 2 + 3);
|
||||
result += "\"";
|
||||
for (const char *c = str; *c != 0; ++c) {
|
||||
switch (*c) {
|
||||
case '\"':
|
||||
result += "\\\"";
|
||||
break;
|
||||
case '\\':
|
||||
result += "\\\\";
|
||||
break;
|
||||
case '\b':
|
||||
result += "\\b";
|
||||
break;
|
||||
case '\t':
|
||||
result += "\\t";
|
||||
break;
|
||||
case '\n':
|
||||
result += "\\n";
|
||||
break;
|
||||
case '\f':
|
||||
result += "\\f";
|
||||
break;
|
||||
case '\r':
|
||||
result += "\\r";
|
||||
break;
|
||||
default:
|
||||
if (is_control_character(*c)) {
|
||||
std::stringstream oss;
|
||||
oss << "\\u" << std::hex << std::uppercase << std::setfill('0')
|
||||
<< std::setw(4) << static_cast<int>(*c);
|
||||
result += oss.str();
|
||||
} else {
|
||||
result += *c;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
result += "\"";
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace json {
|
||||
|
||||
static inline bool is_control_character(char ch)
|
||||
{ return ch > 0 && ch <= 0x1F; }
|
||||
|
||||
std::string escape_string(const char *str);
|
||||
|
||||
}
|
||||
+86
-44
@@ -16,79 +16,121 @@
|
||||
*/
|
||||
|
||||
#include <android/log.h>
|
||||
#include <cassert>
|
||||
#include <iomanip>
|
||||
#include "smsbuffer.h"
|
||||
#include "json.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_JNIMETHOD(createNativeObject, "()J")
|
||||
DECL_JNIMETHOD(deleteNativeObject, "(J)V")
|
||||
DECL_JNIMETHOD(empty, "(J)Z")
|
||||
DECL_JNIMETHOD(asRawJsonString, "(J)Ljava/lang/String;")
|
||||
DECL_JNIMETHOD(push,
|
||||
"(JIIIJLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V")
|
||||
DECL_JNIMETHOD(print, "(J)V")
|
||||
};
|
||||
DECL_METHODSIZE(SmsBuffer)
|
||||
|
||||
#define SMSBUFFER_CAST \
|
||||
SmsBuffer *me = reinterpret_cast<SmsBuffer *>(ptr); \
|
||||
if (!me) { \
|
||||
__android_log_print(ANDROID_LOG_FATAL, LOG_TAG, "It's not a SmsBuffer!"); \
|
||||
assert(false); \
|
||||
}
|
||||
|
||||
jlong SmsBuffer::createNativeObject(JNIEnv *env, jobject self)
|
||||
{
|
||||
return reinterpret_cast<jlong>(new SmsBuffer());
|
||||
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()
|
||||
{
|
||||
__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "deleteSmsBuffer");
|
||||
delete reinterpret_cast<SmsBuffer *>(ptr);
|
||||
}
|
||||
|
||||
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);
|
||||
m_buffer.clear();
|
||||
m_buffer_empty = true;
|
||||
m_sms_count = 0;
|
||||
}
|
||||
|
||||
void SmsBuffer::_push(int mailbox_id)
|
||||
void SmsBuffer::push(JNIEnv *env, jobject self, jlong ptr, jint msg_id, jint mailbox_id, jint type,
|
||||
jlong date, jstring address, jstring body, jstring read, jstring seen)
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
SMSBUFFER_CAST
|
||||
me->_push(msg_id, mailbox_id, type, date, env->GetStringUTFChars(address, NULL),
|
||||
env->GetStringUTFChars(body, NULL), env->GetStringUTFChars(read, NULL),
|
||||
env->GetStringUTFChars(seen, NULL));
|
||||
}
|
||||
|
||||
m_buffer << "{\"mbox\": " << mailbox_id << "}";
|
||||
void SmsBuffer::_push(int msg_id, int mailbox_id, int type,
|
||||
long date, const char *address, const char *body, const char *read,
|
||||
const char *seen)
|
||||
{
|
||||
// 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 = false;
|
||||
}
|
||||
|
||||
m_buffer << "{\"_id\": " << msg_id << ", "
|
||||
<< "\"mbox\": " << mailbox_id << ", "
|
||||
<< "\"type\": " << type << ", "
|
||||
<< "\"date\": " << date << ", "
|
||||
<< "\"body\": " << json::escape_string(body) << ", "
|
||||
<< "\"address\": " << json::escape_string(address) << ", "
|
||||
<< "\"read\": " << json::escape_string(read) << ", "
|
||||
<< "\"seen\": " << json::escape_string(seen)
|
||||
<< "}";
|
||||
m_sms_count++;
|
||||
}
|
||||
|
||||
jboolean SmsBuffer::empty(JNIEnv *env, jobject self, jlong ptr)
|
||||
{
|
||||
SMSBUFFER_CAST
|
||||
return (jboolean) (me->_empty() ? 1 : 0);
|
||||
}
|
||||
|
||||
bool SmsBuffer::_empty() const
|
||||
{
|
||||
return m_buffer_empty;
|
||||
}
|
||||
|
||||
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();
|
||||
SMSBUFFER_CAST
|
||||
me->_print();
|
||||
}
|
||||
|
||||
void SmsBuffer::_print()
|
||||
{
|
||||
__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "SmsBuffer content: '%s'", m_buffer.str().c_str());
|
||||
__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "SmsBuffer content: '%s'",
|
||||
m_buffer.str().c_str());
|
||||
}
|
||||
|
||||
jstring SmsBuffer::asRawJsonString(JNIEnv *env, jobject self, jlong ptr)
|
||||
{
|
||||
SMSBUFFER_CAST
|
||||
std::string result;
|
||||
me->as_raw_json_string(result);
|
||||
return env->NewStringUTF(result.c_str());
|
||||
}
|
||||
|
||||
void SmsBuffer::as_raw_json_string(std::string &result)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "{\"smsCount\": " << m_sms_count << ", \"smsDatas\": " << m_buffer.str() << "]}";
|
||||
result = ss.str();
|
||||
}
|
||||
@@ -24,21 +24,46 @@
|
||||
class SmsBuffer
|
||||
{
|
||||
public:
|
||||
SmsBuffer();
|
||||
SmsBuffer() = default;
|
||||
~SmsBuffer() = default;
|
||||
|
||||
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);
|
||||
/*
|
||||
* push method
|
||||
*/
|
||||
JNIEXPORT static void JNICALL push(JNIEnv *env, jobject self, jlong ptr, jint msg_id,
|
||||
jint mailbox_id, jint type, jlong date, jstring address,
|
||||
jstring body, jstring read, jstring seen);
|
||||
void _push(int msg_id, int mailbox_id, int type,
|
||||
long date, const char *address, const char *body, const char *read,
|
||||
const char *seen);
|
||||
|
||||
/*
|
||||
* empty method
|
||||
*/
|
||||
|
||||
JNIEXPORT static jboolean JNICALL empty(JNIEnv *env, jobject self, jlong ptr);
|
||||
bool _empty() const;
|
||||
|
||||
/*
|
||||
* print method
|
||||
*/
|
||||
JNIEXPORT static void JNICALL print(JNIEnv *env, jobject self, jlong ptr);
|
||||
void _print();
|
||||
|
||||
/*
|
||||
* asRawJsonString method
|
||||
*/
|
||||
JNIEXPORT static jstring JNICALL asRawJsonString(JNIEnv *env, jobject self, jlong ptr);
|
||||
void as_raw_json_string(std::string &result);
|
||||
|
||||
DECL_JNICLASSATTRS
|
||||
|
||||
private:
|
||||
void reset_buffer();
|
||||
std::stringstream m_buffer;
|
||||
uint32_t m_sms_count{0};
|
||||
bool m_buffer_empty{true};
|
||||
};
|
||||
Reference in New Issue
Block a user