forked from daudrain/TestQtAndroidWithClariusCast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
castbridgeandroid.cpp
141 lines (101 loc) · 3.74 KB
/
castbridgeandroid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "castbridgeandroid.h"
#include <QDebug>
#define __ANDROID__ 1
#ifdef __ANDROID__
#include <QtCore/private/qandroidextras_p.h>
void onTestCallBack(JNIEnv*, jobject, jboolean value, jlong obj) {
CastBridgeAndroid *instance = reinterpret_cast<CastBridgeAndroid*>(obj);
qDebug()<<"<<callback instance " << instance;
qDebug()<<"<<onTestCallBack c++ " << value <<", secret value : " << instance->secretValue;
}
CastBridgeAndroid::CastBridgeAndroid(QObject *parent)
: CastBridge{parent}
{
env_ = new QJniEnvironment();
}
CastBridgeAndroid::~CastBridgeAndroid() {
if (castHandle_) delete castHandle_;
delete env_;
}
void CastBridgeAndroid::lowConnect(QString ip, int port, long networkID, QString certificate)
{
qDebug()<<"<<lowConnect ";
stackResult("lowConnect");
if (!castHandle_)
lowCreate(false);
auto jip = QJniObject::fromString(ip);
auto jcertificate = QJniObject::fromString(certificate);
/*
auto succeeded = castHandle_->callMethod<jboolean>("connect",
"(Ljava/lang/String;IJLjava/lang/String;)Z", jip.object(), port, networkID, jcertificate.object());
*/
/*
auto callback = [](QString& data){
qDebug()<<"<<callback " << data;
};
*/
JNINativeMethod methods[]{
{ "onConnected", "(ZJ)V", reinterpret_cast<void*>(onTestCallBack) }
};
jclass javaClass = env_->findClass("test/cast/CastWrapper");
qDebug()<<" sizeof(methods) " << sizeof(methods) << ", sizeof(methods[0])" << sizeof(methods[0]);
const int result = env_->registerNativeMethods(javaClass, methods, 1);
qDebug()<<"<<registerNativeMethods " << result;
secretValue = 132;
jlong obj = reinterpret_cast<jlong>(this);
auto succeeded = castHandle_->callMethod<jboolean>("test","(Ljava/lang/String;IJJ)Z",jip.object(),port, networkID, obj);
qDebug()<<"<<this " << this;
qDebug()<<"<<succeeded " << succeeded;
stackResult(QString("succeeded?") + QChar(succeeded));
}
bool CastBridgeAndroid::lowCreate(bool anotherThread) {
qDebug()<<">>cast.create " << (anotherThread ? "from another thread" : "from same thread");
if (!castHandle_) {
jclass javaClass = env_->findClass("test/cast/CastWrapper");
if (javaClass == NULL) {
qWarning() << "WARNING: Failed to find class "
"test/cast/Test";
return false;
}
castHandle_ = new QJniObject(javaClass,"(Z)V", anotherThread);
}
QJniObject ctx = QNativeInterface::QAndroidApplication::context();
auto succeeded = castHandle_->callMethod<jboolean>("createCast",
"(Landroid/content/Context;)Z",
ctx.object());
qDebug()<<"<<cast.create "<<succeeded;
return succeeded;
}
bool CastBridgeAndroid::lowRelease() {
if (!castHandle_) return false;
qDebug()<<">>cast.release";
auto succeeded = castHandle_->callMethod<jboolean>("releaseCast",
"()Z");
delete castHandle_;
castHandle_ = nullptr;
qDebug()<<"<<cast.release "<<succeeded;
return succeeded;
}
#else
CastBridgeAndroid::CastBridgeAndroid(QObject *parent)
: CastBridge{parent}
{
}
CastBridgeAndroid::~CastBridgeAndroid() {
}
void CastBridgeAndroid::lowConnect(QString ip, int port, long networkID, QString certificate)
{
qDebug()<<"<<lowConnect " << result_;
stackResult("lowConnect");
}
bool CastBridgeAndroid::lowCreate(bool anotherThread) {
return false;
}
bool CastBridgeAndroid::lowRelease() {
return false;
}
#endif
void CastBridgeAndroid::stackResult(QString message){
result_ = message + "\n" + result_;
emit resultChanged(result_);
}