Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4d67d31

Browse filesBrowse files
committed
New androidnotification addon
only implements `fcitx::INotifications::showTip`
1 parent 93f25a8 commit 4d67d31
Copy full SHA for 4d67d31

File tree

Expand file treeCollapse file tree

13 files changed

+227
-3
lines changed
Filter options
Expand file treeCollapse file tree

13 files changed

+227
-3
lines changed

‎.idea/dictionaries/project.xml

Copy file name to clipboardExpand all lines: .idea/dictionaries/project.xml
+1Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎app/build.gradle.kts

Copy file name to clipboardExpand all lines: app/build.gradle.kts
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ android {
2727
"copy-fcitx5-modules",
2828
// android specific modules
2929
"androidfrontend",
30-
"androidkeyboard"
30+
"androidkeyboard",
31+
"androidnotification"
3132
)
3233
}
3334
}

‎app/src/main/cpp/CMakeLists.txt

Copy file name to clipboardExpand all lines: app/src/main/cpp/CMakeLists.txt
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ include("${FCITX_INSTALL_CMAKECONFIG_DIR}/Fcitx5Utils/Fcitx5CompilerSettings.cma
2828
add_subdirectory(po)
2929
add_subdirectory(androidfrontend)
3030
add_subdirectory(androidkeyboard)
31+
add_subdirectory(androidnotification)
3132

3233
# prebuilt dir. at least it works.
3334
set(PREBUILT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../lib/fcitx5/src/main/cpp/prebuilt")

‎app/src/main/cpp/androidfrontend/androidfrontend.cpp

Copy file name to clipboardExpand all lines: app/src/main/cpp/androidfrontend/androidfrontend.cpp
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ std::vector<std::string> AndroidFrontend::getCandidates(const int offset, const
296296
return ic->getCandidates(offset, limit);
297297
}
298298

299+
void AndroidFrontend::showToast(const std::string &s) {
300+
toastCallback(s);
301+
}
302+
299303
void AndroidFrontend::setCommitStringCallback(const CommitStringCallback &callback) {
300304
commitStringCallback = callback;
301305
}
@@ -331,6 +335,10 @@ void AndroidFrontend::handleStatusAreaUpdate() {
331335
});
332336
}
333337

338+
void AndroidFrontend::setToastCallback(const ToastCallback &callback) {
339+
toastCallback = callback;
340+
}
341+
334342
class AndroidFrontendFactory : public AddonFactory {
335343
public:
336344
AddonInstance *create(AddonManager *manager) override {

‎app/src/main/cpp/androidfrontend/androidfrontend.h

Copy file name to clipboardExpand all lines: app/src/main/cpp/androidfrontend/androidfrontend.h
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include <fcitx/instance.h>
55
#include <fcitx/addoninstance.h>
6-
#include <fcitx/inputcontext.h>
76
#include <fcitx-utils/i18n.h>
87

98
#include "androidfrontend_public.h"
@@ -35,13 +34,15 @@ class AndroidFrontend : public AddonInstance {
3534
InputContext *activeInputContext() const;
3635
void setCapabilityFlags(uint64_t flag);
3736
std::vector<std::string> getCandidates(const int offset, const int limit);
37+
void showToast(const std::string &s);
3838
void setCandidateListCallback(const CandidateListCallback &callback);
3939
void setCommitStringCallback(const CommitStringCallback &callback);
4040
void setPreeditCallback(const ClientPreeditCallback &callback);
4141
void setInputPanelAuxCallback(const InputPanelCallback &callback);
4242
void setKeyEventCallback(const KeyEventCallback &callback);
4343
void setInputMethodChangeCallback(const InputMethodChangeCallback &callback);
4444
void setStatusAreaUpdateCallback(const StatusAreaUpdateCallback &callback);
45+
void setToastCallback(const ToastCallback &callback);
4546

4647
private:
4748
FCITX_ADDON_EXPORT_FUNCTION(AndroidFrontend, keyEvent);
@@ -55,13 +56,15 @@ class AndroidFrontend : public AddonInstance {
5556
FCITX_ADDON_EXPORT_FUNCTION(AndroidFrontend, deactivateInputContext);
5657
FCITX_ADDON_EXPORT_FUNCTION(AndroidFrontend, setCapabilityFlags);
5758
FCITX_ADDON_EXPORT_FUNCTION(AndroidFrontend, getCandidates);
59+
FCITX_ADDON_EXPORT_FUNCTION(AndroidFrontend, showToast);
5860
FCITX_ADDON_EXPORT_FUNCTION(AndroidFrontend, setCandidateListCallback);
5961
FCITX_ADDON_EXPORT_FUNCTION(AndroidFrontend, setCommitStringCallback);
6062
FCITX_ADDON_EXPORT_FUNCTION(AndroidFrontend, setPreeditCallback);
6163
FCITX_ADDON_EXPORT_FUNCTION(AndroidFrontend, setInputPanelAuxCallback);
6264
FCITX_ADDON_EXPORT_FUNCTION(AndroidFrontend, setKeyEventCallback);
6365
FCITX_ADDON_EXPORT_FUNCTION(AndroidFrontend, setInputMethodChangeCallback);
6466
FCITX_ADDON_EXPORT_FUNCTION(AndroidFrontend, setStatusAreaUpdateCallback);
67+
FCITX_ADDON_EXPORT_FUNCTION(AndroidFrontend, setToastCallback);
6568

6669
Instance *instance_;
6770
FocusGroup focusGroup_;
@@ -80,6 +83,7 @@ class AndroidFrontend : public AddonInstance {
8083
KeyEventCallback keyEventCallback = [](const int, const uint32_t, const uint32_t, const bool, const int) {};
8184
InputMethodChangeCallback imChangeCallback = [] {};
8285
StatusAreaUpdateCallback statusAreaUpdateCallback = [] {};
86+
ToastCallback toastCallback = [](const std::string &) {};
8387
};
8488
} // namespace fcitx
8589

‎app/src/main/cpp/androidfrontend/androidfrontend_public.h

Copy file name to clipboardExpand all lines: app/src/main/cpp/androidfrontend/androidfrontend_public.h
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef _FCITX5_ANDROID_ANDROIDFRONTEND_PUBLIC_H_
22
#define _FCITX5_ANDROID_ANDROIDFRONTEND_PUBLIC_H_
33

4-
#include <fcitx/text.h>
4+
#include <fcitx/inputcontext.h>
55
#include <fcitx-utils/key.h>
66

77
typedef std::function<void(const std::vector<std::string> &, const int)> CandidateListCallback;
@@ -11,6 +11,7 @@ typedef std::function<void(const fcitx::Text &, const fcitx::Text &, const fcitx
1111
typedef std::function<void(const int, const uint32_t, const uint32_t, const bool, const int)> KeyEventCallback;
1212
typedef std::function<void()> InputMethodChangeCallback;
1313
typedef std::function<void()> StatusAreaUpdateCallback;
14+
typedef std::function<void(const std::string &)> ToastCallback;
1415

1516
FCITX_ADDON_DECLARE_FUNCTION(AndroidFrontend, keyEvent,
1617
void(const fcitx::Key &, bool isRelease, const int timestamp))
@@ -45,6 +46,9 @@ FCITX_ADDON_DECLARE_FUNCTION(AndroidFrontend, setCapabilityFlags,
4546
FCITX_ADDON_DECLARE_FUNCTION(AndroidFrontend, getCandidates,
4647
std::vector<std::string>(const int, const int))
4748

49+
FCITX_ADDON_DECLARE_FUNCTION(AndroidFrontend, showToast,
50+
void(const std::string &))
51+
4852
FCITX_ADDON_DECLARE_FUNCTION(AndroidFrontend, setCandidateListCallback,
4953
void(const CandidateListCallback &))
5054

@@ -66,4 +70,7 @@ FCITX_ADDON_DECLARE_FUNCTION(AndroidFrontend, setInputMethodChangeCallback,
6670
FCITX_ADDON_DECLARE_FUNCTION(AndroidFrontend, setStatusAreaUpdateCallback,
6771
void(const StatusAreaUpdateCallback &))
6872

73+
FCITX_ADDON_DECLARE_FUNCTION(AndroidFrontend, setToastCallback,
74+
void(const ToastCallback &))
75+
6976
#endif // _FCITX5_ANDROID_ANDROIDFRONTEND_PUBLIC_H_
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
add_definitions(-DFCITX_GETTEXT_DOMAIN=\"fcitx5-android\")
2+
3+
add_library(androidnotification MODULE androidnotification.cpp)
4+
target_link_libraries(androidnotification Fcitx5::Core Fcitx5::Utils Fcitx5::Module::Notifications)
5+
6+
configure_file(notifications.conf.in.in notifications.conf.in @ONLY)
7+
fcitx5_translate_desktop_file(${CMAKE_CURRENT_BINARY_DIR}/notifications.conf.in notifications.conf)
8+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/notifications.conf"
9+
DESTINATION "${FCITX_INSTALL_PKGDATADIR}/addon"
10+
COMPONENT config)
+85Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <jni.h>
2+
3+
#include <fcitx/addonfactory.h>
4+
#include <fcitx/addonmanager.h>
5+
6+
#include "../androidfrontend/androidfrontend_public.h"
7+
8+
#include "androidnotification.h"
9+
10+
namespace fcitx {
11+
12+
void Notifications::updateConfig() {
13+
hiddenNotifications_.clear();
14+
for (const auto &id: config_.hiddenNotifications.value()) {
15+
hiddenNotifications_.insert(id);
16+
}
17+
}
18+
19+
void Notifications::reloadConfig() {
20+
readAsIni(config_, ConfPath);
21+
updateConfig();
22+
}
23+
24+
void Notifications::save() {
25+
std::vector<std::string> values_;
26+
for (const auto &id: hiddenNotifications_) {
27+
values_.push_back(id);
28+
}
29+
config_.hiddenNotifications.setValue(std::move(values_));
30+
safeSaveAsIni(config_, ConfPath);
31+
}
32+
33+
uint32_t Notifications::sendNotification(
34+
const std::string &appName,
35+
uint32_t replaceId,
36+
const std::string &appIcon,
37+
const std::string &summary,
38+
const std::string &body,
39+
const std::vector<std::string> &actions,
40+
int32_t timeout,
41+
NotificationActionCallback actionCallback,
42+
NotificationClosedCallback closedCallback) {
43+
// TODO implement Notification
44+
FCITX_UNUSED(appName);
45+
FCITX_UNUSED(replaceId);
46+
FCITX_UNUSED(appIcon);
47+
FCITX_UNUSED(summary);
48+
FCITX_UNUSED(body);
49+
FCITX_UNUSED(actions);
50+
FCITX_UNUSED(timeout);
51+
FCITX_UNUSED(actionCallback);
52+
FCITX_UNUSED(closedCallback);
53+
return 0;
54+
}
55+
56+
void Notifications::showTip(
57+
const std::string &tipId,
58+
const std::string &appName,
59+
const std::string &appIcon,
60+
const std::string &summary,
61+
const std::string &body,
62+
int32_t timeout) {
63+
FCITX_UNUSED(appName);
64+
FCITX_UNUSED(appIcon);
65+
FCITX_UNUSED(timeout);
66+
if (hiddenNotifications_.count(tipId)) {
67+
return;
68+
}
69+
std::string const s = summary + ": " + body;
70+
androidfrontend()->call<IAndroidFrontend::showToast>(s);
71+
}
72+
73+
void Notifications::closeNotification(uint64_t internalId) {
74+
FCITX_UNUSED(internalId);
75+
}
76+
77+
class NotificationsModuleFactory : public AddonFactory {
78+
AddonInstance *create(AddonManager *manager) override {
79+
return new Notifications(manager->instance());
80+
}
81+
};
82+
83+
}
84+
85+
FCITX_ADDON_FACTORY(fcitx::NotificationsModuleFactory)
+75Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#ifndef FCITX5_ANDROID_ANDROIDNOTIFICATION_H
2+
#define FCITX5_ANDROID_ANDROIDNOTIFICATION_H
3+
4+
#include <functional>
5+
#include <unordered_set>
6+
#include <utility>
7+
#include <fcitx-config/configuration.h>
8+
#include <fcitx-config/iniparser.h>
9+
#include <fcitx-utils/fs.h>
10+
#include <fcitx-utils/i18n.h>
11+
#include <fcitx/addoninstance.h>
12+
#include <fcitx/instance.h>
13+
14+
#include <notifications_public.h>
15+
16+
namespace fcitx {
17+
18+
FCITX_CONFIGURATION(NotificationsConfig,
19+
fcitx::Option<std::vector<std::string>> hiddenNotifications{
20+
this, "HiddenNotifications",
21+
_("Hidden Notifications")};);
22+
23+
class Notifications final : public AddonInstance {
24+
public:
25+
Notifications(Instance *instance) : instance_(instance) {};
26+
~Notifications() = default;
27+
28+
Instance *instance() { return instance_; }
29+
30+
void updateConfig();
31+
void reloadConfig() override;
32+
void save() override;
33+
34+
const Configuration *getConfig() const override { return &config_; }
35+
36+
void setConfig(const RawConfig &config) override {
37+
config_.load(config, true);
38+
safeSaveAsIni(config_, ConfPath);
39+
updateConfig();
40+
}
41+
42+
FCITX_ADDON_DEPENDENCY_LOADER(androidfrontend, instance_->addonManager());
43+
44+
uint32_t sendNotification(const std::string &appName, uint32_t replaceId,
45+
const std::string &appIcon,
46+
const std::string &summary,
47+
const std::string &body,
48+
const std::vector<std::string> &actions,
49+
int32_t timeout,
50+
NotificationActionCallback actionCallback,
51+
NotificationClosedCallback closedCallback);
52+
53+
void showTip(const std::string &tipId, const std::string &appName,
54+
const std::string &appIcon, const std::string &summary,
55+
const std::string &body, int32_t timeout);
56+
57+
void closeNotification(uint64_t internalId);
58+
59+
private:
60+
FCITX_ADDON_EXPORT_FUNCTION(Notifications, sendNotification);
61+
FCITX_ADDON_EXPORT_FUNCTION(Notifications, showTip);
62+
FCITX_ADDON_EXPORT_FUNCTION(Notifications, closeNotification);
63+
64+
static const inline std::string ConfPath = "conf/androidnotification.conf";
65+
66+
NotificationsConfig config_;
67+
Instance *instance_;
68+
69+
std::unordered_set<std::string> hiddenNotifications_;
70+
71+
}; // class Notifications
72+
73+
} // namespace fcitx
74+
75+
#endif //FCITX5_ANDROID_ANDROIDNOTIFICATION_H
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Addon]
2+
Name=Android Toast & Notification
3+
Type=SharedLibrary
4+
Library=libandroidnotification
5+
Category=Module
6+
Version=@PROJECT_VERSION@
7+
OnDemand=True
8+
Configurable=True
9+
10+
[Addon/Dependencies]
11+
0=androidfrontend:@PROJECT_VERSION@

‎app/src/main/cpp/jni-utils.h

Copy file name to clipboardExpand all lines: app/src/main/cpp/jni-utils.h
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class GlobalRefSingleton {
102102
jmethodID BooleanInit;
103103

104104
jclass Fcitx;
105+
jmethodID ShowToast;
105106
jmethodID HandleFcitxEvent;
106107

107108
jclass InputMethodEntry;
@@ -142,6 +143,7 @@ class GlobalRefSingleton {
142143
BooleanInit = env->GetMethodID(Boolean, "<init>", "(Z)V");
143144

144145
Fcitx = reinterpret_cast<jclass>(env->NewGlobalRef(env->FindClass("org/fcitx/fcitx5/android/core/Fcitx")));
146+
ShowToast = env->GetStaticMethodID(Fcitx, "showToast", "(Ljava/lang/String;)V");
145147
HandleFcitxEvent = env->GetStaticMethodID(Fcitx, "handleFcitxEvent", "(I[Ljava/lang/Object;)V");
146148

147149
InputMethodEntry = reinterpret_cast<jclass>(env->NewGlobalRef(env->FindClass("org/fcitx/fcitx5/android/core/InputMethodEntry")));

‎app/src/main/cpp/native-lib.cpp

Copy file name to clipboardExpand all lines: app/src/main/cpp/native-lib.cpp
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,10 @@ Java_org_fcitx_fcitx5_android_core_Fcitx_startupFcitx(JNIEnv *env, jclass clazz,
615615
}
616616
env->CallStaticVoidMethod(GlobalRef->Fcitx, GlobalRef->HandleFcitxEvent, 7, *vararg);
617617
};
618+
auto toastCallback = [](const std::string &s) {
619+
auto env = GlobalRef->AttachEnv();
620+
env->CallStaticVoidMethod(GlobalRef->Fcitx, GlobalRef->ShowToast, *JString(env, s));
621+
};
618622

619623
Fcitx::Instance().startup([&](auto *androidfrontend) {
620624
FCITX_INFO() << "Setting up callback";
@@ -626,6 +630,7 @@ Java_org_fcitx_fcitx5_android_core_Fcitx_startupFcitx(JNIEnv *env, jclass clazz,
626630
androidfrontend->template call<fcitx::IAndroidFrontend::setKeyEventCallback>(keyEventCallback);
627631
androidfrontend->template call<fcitx::IAndroidFrontend::setInputMethodChangeCallback>(imChangeCallback);
628632
androidfrontend->template call<fcitx::IAndroidFrontend::setStatusAreaUpdateCallback>(statusAreaUpdateCallback);
633+
androidfrontend->template call<fcitx::IAndroidFrontend::setToastCallback>(toastCallback);
629634
});
630635
FCITX_INFO() << "Finishing startup";
631636
}

‎app/src/main/java/org/fcitx/fcitx5/android/core/Fcitx.kt

Copy file name to clipboardExpand all lines: app/src/main/java/org/fcitx/fcitx5/android/core/Fcitx.kt
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.fcitx.fcitx5.android.core
22

33
import android.content.Context
44
import androidx.annotation.Keep
5+
import androidx.core.content.ContextCompat
56
import kotlinx.coroutines.channels.BufferOverflow
67
import kotlinx.coroutines.flow.MutableSharedFlow
78
import kotlinx.coroutines.flow.asSharedFlow
@@ -15,6 +16,8 @@ import org.fcitx.fcitx5.android.data.clipboard.ClipboardManager
1516
import org.fcitx.fcitx5.android.data.prefs.AppPrefs
1617
import org.fcitx.fcitx5.android.utils.ImmutableGraph
1718
import org.fcitx.fcitx5.android.utils.Locales
19+
import org.fcitx.fcitx5.android.utils.appContext
20+
import org.fcitx.fcitx5.android.utils.toast
1821
import timber.log.Timber
1922

2023
/**
@@ -166,6 +169,17 @@ class Fcitx(private val context: Context) : FcitxAPI, FcitxLifecycleOwner {
166169

167170
private companion object JNI {
168171

172+
/**
173+
* called from native-lib
174+
*/
175+
@Suppress("unused")
176+
@JvmStatic
177+
fun showToast(s: String) {
178+
ContextCompat.getMainExecutor(appContext).execute {
179+
appContext.toast(s)
180+
}
181+
}
182+
169183
private val eventFlow_ =
170184
MutableSharedFlow<FcitxEvent<*>>(
171185
extraBufferCapacity = 15,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.