-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge #331: Use node model state to update the android notification
b641df4 android: use node model state to update notification (johnny9) Pull request description: The AndroidNotifier class connects to the NodeModel's state signals and uses JNI to send callbacks to the Android service managing the foreground notification. [![Windows](https://img.shields.io/badge/OS-Windows-green)](https://api.cirrus-ci.com/v1/artifact/github/bitcoin-core/gui-qml/win64/unsecure_win_gui.zip?branch=pull/331) [![Intel macOS](https://img.shields.io/badge/OS-Intel%20macOS-green)](https://api.cirrus-ci.com/v1/artifact/github/bitcoin-core/gui-qml/macos/unsecure_mac_gui.zip?branch=pull/331) [![Apple Silicon macOS](https://img.shields.io/badge/OS-Apple%20Silicon%20macOS-green)](https://api.cirrus-ci.com/v1/artifact/github/bitcoin-core/gui-qml/macos_arm64/unsecure_mac_arm64_gui.zip?branch=pull/331) [![ARM64 Android](https://img.shields.io/badge/OS-Android-green)](https://api.cirrus-ci.com/v1/artifact/github/bitcoin-core/gui-qml/android/unsecure_android_apk.zip?branch=pull/331) ACKs for top commit: jarolrod: ACK b641df4 Tree-SHA512: 1dfc95ade573f6620ba67871ff6f609e4bffe4cee877e03bd56c8917ac8cfb184198e5ca603ffda1962a3416da52921c0228eab5f0172011eca44de3d813e0d0
- Loading branch information
Showing
7 changed files
with
232 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright (c) 2023 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <qml/androidnotifier.h> | ||
|
||
#include <jni.h> | ||
|
||
extern "C" { | ||
JNIEXPORT jboolean JNICALL Java_org_bitcoincore_qt_BitcoinQtService_register(JNIEnv *env, jobject obj); | ||
} | ||
|
||
static JavaVM * g_vm = nullptr; | ||
static jobject g_obj; | ||
|
||
JNIEXPORT jboolean JNICALL Java_org_bitcoincore_qt_BitcoinQtService_register(JNIEnv * env, jobject obj) | ||
{ | ||
env->GetJavaVM(&g_vm); | ||
g_obj = env->NewGlobalRef(obj); | ||
|
||
return (jboolean) true; | ||
} | ||
|
||
namespace { | ||
|
||
JNIEnv* getJNIEnv(JavaVM* javaVM) { | ||
JNIEnv* env; | ||
jint result = javaVM->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6); | ||
|
||
if (result == JNI_EDETACHED) { | ||
javaVM->AttachCurrentThread(&env, nullptr); | ||
} else if (result != JNI_OK) { | ||
// Error handling | ||
return nullptr; | ||
} | ||
|
||
return env; | ||
} | ||
|
||
} | ||
|
||
AndroidNotifier::AndroidNotifier(const NodeModel & node_model, | ||
QObject * parent) | ||
: QObject(parent) | ||
, m_node_model(node_model) | ||
{ | ||
QObject::connect(&node_model, &NodeModel::blockTipHeightChanged, | ||
this, &AndroidNotifier::onBlockTipHeightChanged); | ||
QObject::connect(&node_model, &NodeModel::numOutboundPeersChanged, | ||
this, &AndroidNotifier::onNumOutboundPeersChanged); | ||
QObject::connect(&node_model, &NodeModel::pauseChanged, | ||
this, &AndroidNotifier::onPausedChanged); | ||
QObject::connect(&node_model, &NodeModel::verificationProgressChanged, | ||
this, &AndroidNotifier::onVerificationProgressChanged); | ||
} | ||
|
||
void AndroidNotifier::onBlockTipHeightChanged() | ||
{ | ||
if (g_vm != nullptr) { | ||
JNIEnv * env = getJNIEnv(g_vm); | ||
if (env == nullptr) { | ||
return; | ||
} | ||
jclass clazz = env->GetObjectClass(g_obj); | ||
jmethodID mid = env->GetMethodID(clazz, "updateBlockTipHeight", "(I)V"); | ||
env->CallVoidMethod(g_obj, mid, m_node_model.blockTipHeight()); | ||
} | ||
} | ||
|
||
void AndroidNotifier::onNumOutboundPeersChanged() | ||
{ | ||
if (g_vm != nullptr) { | ||
JNIEnv * env = getJNIEnv(g_vm); | ||
if (env == nullptr) { | ||
return; | ||
} | ||
jclass clazz = env->GetObjectClass(g_obj); | ||
jmethodID mid = env->GetMethodID(clazz, "updateNumberOfPeers", "(I)V"); | ||
env->CallVoidMethod(g_obj, mid, m_node_model.numOutboundPeers()); | ||
} | ||
} | ||
|
||
void AndroidNotifier::onVerificationProgressChanged() | ||
{ | ||
if (g_vm != nullptr) { | ||
JNIEnv * env = getJNIEnv(g_vm); | ||
if (env == nullptr) { | ||
return; | ||
} | ||
jclass clazz = env->GetObjectClass(g_obj); | ||
jmethodID mid = env->GetMethodID(clazz, "updateVerificationProgress", "(D)V"); | ||
env->CallVoidMethod(g_obj, mid, static_cast<jdouble>(m_node_model.verificationProgress())); | ||
} | ||
} | ||
|
||
void AndroidNotifier::onPausedChanged() | ||
{ | ||
if (g_vm != nullptr) { | ||
JNIEnv * env = getJNIEnv(g_vm); | ||
if (env == nullptr) { | ||
return; | ||
} | ||
jclass clazz = env->GetObjectClass(g_obj); | ||
jmethodID mid = env->GetMethodID(clazz, "updatePaused", "(Z)V"); | ||
env->CallVoidMethod(g_obj, mid, static_cast<jboolean>(m_node_model.pause())); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef BITCOIN_QML_ANDROIDNOTIFIER_H | ||
#define BITCOIN_QML_ANDROIDNOTIFIER_H | ||
|
||
#include <qml/models/nodemodel.h> | ||
|
||
#include <QObject> | ||
#include <jni.h> | ||
|
||
class AndroidNotifier : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit AndroidNotifier(const NodeModel & node_model, QObject * parent = nullptr); | ||
|
||
public Q_SLOTS: | ||
void onBlockTipHeightChanged(); | ||
void onNumOutboundPeersChanged(); | ||
void onVerificationProgressChanged(); | ||
void onPausedChanged(); | ||
|
||
private: | ||
const NodeModel & m_node_model; | ||
}; | ||
|
||
#endif // BITCOIN_QML_ANDROIDNOTIFIER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters