mcu_hi3321_watch/tjd/ui/common/TjdUiMsgCenter.cpp
2025-05-26 20:15:20 +08:00

261 lines
9.1 KiB
C++

/*----------------------------------------------------------------------------
* Copyright (c) TJD Technologies Co., Ltd. 2024. All rights reserved.
*
* Description:
*
* Author: huangshuyi
*
* Create: 2024-8
*--------------------------------------------------------------------------*/
#include "TjdUiMsgCenter.h"
#include "NativeAbility.h"
#include "TjdUiAppIds.h"
#include "abilityms_slite_client.h"
#include "adapter.h"
#include "broadcast_service.h"
#include "common_def.h"
#include "element_name.h"
#include "memory_adapter.h"
#include "msg_center.h"
#include "notification_manager.h"
#include "ohos_init.h"
#include "power_display_service.h"
#include "samgr_lite.h"
#include "securec.h"
#include "service_id_define.h"
#include "want.h"
#include "wearable_log.h"
#include <cstdio>
#include <list>
//#include "TjdUiMessagePopUpPage.h"
extern void TjdUiMsgCenterMessageProc(const Topic topic, Request *req);
extern void TjdUiMsgCenterFemaleProc(const Topic topic, Request *req);
namespace TJD {
extern void TjdUiPhoneMsgCenterMessageProc(const Topic topic, Request *req);
extern void TjdUiChargerMsgCenterMessageProc(const Topic topic, Request *req);
extern void TjdUiShutdownMsgCenterMessageProc(const Topic topic, Request *req);
extern void TjdUiMsgCenterAlarmProc(const Topic topic, Request *req);
extern void TjdUiSportMessageProc(const Topic topic, Request *req);
extern void TjdUiLuckyCloverMessageProc(const Topic topic, Request *req);
extern void TjdUiLowPowerMsgCenterMessageProc(const Topic topic, Request *req);
extern void TjdUiCameraMsgCenterMessageProc(const Topic topic, Request *req);
extern void TjdUiFindDeviceMsgCenterMessageProc(const Topic topic, Request *req);
extern void TjdUiActivityRecognitionMessageProc(const Topic topic, Request *req);
} // namespace TJD
struct TjdUiPriorityMapper
{
uint16_t topicId;
TjdUiAppPriority prId; // 优先级级别
};
/* 订阅事件设定优先级 */
const TjdUiPriorityMapper TJD_PRI_MAPPER[] = {
{TJDUI_TOPIC_EVENT_HFP_INCOMMING, TJD_UI_PRIO_1}, {TJDUI_TOPIC_EVENT_SHUTDOWN, TJD_UI_PRIO_2},
{TJDUI_TOPIC_EVENT_CHARING, TJD_UI_PRIO_4}, {TJDUI_TOPIC_EVENT_MSG, TJD_UI_PRIO_5},
{TJDUI_TOPIC_EVENT_FEMALE, TJD_UI_PRIO_5}, {TJDUI_TOPIC_EVENT_ALARM, TJD_UI_PRIO_3},
{TJDUI_TOPIC_EVENT_SPORT, TJD_UI_PRIO_4}, {TJDUI_TOPIC_EVENT_LUCKY_CLOVER, TJD_UI_PRIO_5},
{TJDUI_TOPIC_EVENT_LOW_POWER, TJD_UI_PRIO_4}, {TJDUI_TOPIC_EVENT_CAMERA, TJD_UI_PRIO_5},
{TJDUI_TOPIC_EVENT_FIND_DEVICE, TJD_UI_PRIO_5}, {TJDUI_TOPIC_EVENT_ACTIVITY_RECOGNITION, TJD_UI_PRIO_5},
};
/* 订阅事件通知逻辑可定制 */
static MsgCenterNotifyTable g_msgCenterNotifyTable[] = {
{TJDUI_TOPIC_EVENT_MSG, TjdUiMsgCenterMessageProc},
{TJDUI_TOPIC_EVENT_HFP_INCOMMING, TJD::TjdUiPhoneMsgCenterMessageProc},
{TJDUI_TOPIC_EVENT_FEMALE, TjdUiMsgCenterFemaleProc},
{TJDUI_TOPIC_EVENT_CHARING, TJD::TjdUiChargerMsgCenterMessageProc},
{TJDUI_TOPIC_EVENT_SHUTDOWN, TJD::TjdUiShutdownMsgCenterMessageProc},
{TJDUI_TOPIC_EVENT_ALARM, TJD::TjdUiMsgCenterAlarmProc},
{TJDUI_TOPIC_EVENT_SPORT, TJD::TjdUiSportMessageProc},
{TJDUI_TOPIC_EVENT_LUCKY_CLOVER, TJD::TjdUiLuckyCloverMessageProc},
{TJDUI_TOPIC_EVENT_LOW_POWER, TJD::TjdUiLowPowerMsgCenterMessageProc},
{TJDUI_TOPIC_EVENT_CAMERA, TJD::TjdUiCameraMsgCenterMessageProc},
{TJDUI_TOPIC_EVENT_FIND_DEVICE, TJD::TjdUiFindDeviceMsgCenterMessageProc},
{TJDUI_TOPIC_EVENT_ACTIVITY_RECOGNITION, TJD::TjdUiActivityRecognitionMessageProc},
};
static Topic CurTopic = TOPIC_END_NUM;
static std::list<PopPageNotifyCallback> g_pop_in_callback;
static std::list<PopPageNotifyCallback> g_pop_out_callback;
static TjdUiMsgCenterEventCallback g_msgCenterEventCallback;
void RegisterNotifyPopInPage(PopPageNotifyCallback callback)
{
if (callback != nullptr) {
g_pop_in_callback.push_back(callback);
}
}
void DeRegisterNotifyPopInPage(PopPageNotifyCallback callback)
{
if (callback != nullptr) {
g_pop_in_callback.remove(callback);
}
}
void RegisterNotifyPopOutPage(PopPageNotifyCallback callback)
{
if (callback != nullptr) {
g_pop_out_callback.push_back(callback);
}
}
void DeRegisterNotifyPopOutPage(PopPageNotifyCallback callback)
{
if (callback != nullptr) {
g_pop_out_callback.remove(callback);
}
}
void RunPopOutPageCallback(void)
{
CurTopic = TOPIC_END_NUM;
for (auto callback : g_pop_out_callback) {
callback();
}
}
Topic GetCurTopic(void) { return CurTopic; }
static uint8_t MsgCenterGetNotifyTblSize(void)
{
return sizeof(g_msgCenterNotifyTable) / sizeof(g_msgCenterNotifyTable[0]);
}
static MsgCenterNotifyTable *MsgCenterGetNotifyTblCb(uint8_t idx) { return &g_msgCenterNotifyTable[idx]; }
static bool CheckPriority(const Topic topic)
{
uint16_t mapperSize = sizeof(TJD_PRI_MAPPER) / sizeof(TJD_PRI_MAPPER[0]);
for (uint16_t i = 0; i < mapperSize; i++) {
if (topic == TJD_PRI_MAPPER[i].topicId) {
if (TJD_PRI_MAPPER[i].prId <= OHOS::NativeAbility::GetInstance().GetCurSlicePriority() &&
TJD_PRI_MAPPER[i].prId <= OHOS::NotificationManager::GetInstance()->GetNotifyPriority()) {
return true;
} else {
return false;
}
}
}
return false;
}
bool TjdUiMsgCenterNotifyProc(const Topic topic, Request *req)
{
uint8_t i;
uint8_t tblSize = MsgCenterGetNotifyTblSize();
// TjdUiMsgCenterMessageProc(topic, req);
printf("TjdUiMsgCenterNotifyProc topic=%u!!\n", topic);
for (i = 0; i < tblSize; i++) {
MsgCenterNotifyTable *item = MsgCenterGetNotifyTblCb(i);
if (item->topic == topic && item->cb != nullptr) {
if (CheckPriority(topic)) {
CurTopic = topic;
for (auto callback : g_pop_in_callback) {
callback();
}
item->cb(topic, req);
}
return true;
}
}
return false;
}
void TjdUiMsgCenterSubscribe()
{
MsgCenterSubscribeTopic(TJDUI_TOPIC_EVENT_HFP_INCOMMING);
MsgCenterSubscribeTopic(TJDUI_TOPIC_EVENT_CHARING);
MsgCenterSubscribeTopic(TJDUI_TOPIC_EVENT_SHUTDOWN);
MsgCenterSubscribeTopic(TJDUI_TOPIC_EVENT_MSG);
MsgCenterSubscribeTopic(TJDUI_TOPIC_EVENT_FEMALE);
MsgCenterSubscribeTopic(TJDUI_TOPIC_EVENT_ALARM);
MsgCenterSubscribeTopic(TJDUI_TOPIC_EVENT_SPORT);
MsgCenterSubscribeTopic(TJDUI_TOPIC_EVENT_LUCKY_CLOVER);
MsgCenterSubscribeTopic(TJDUI_TOPIC_EVENT_LOW_POWER);
MsgCenterSubscribeTopic(TJDUI_TOPIC_EVENT_CAMERA);
MsgCenterSubscribeTopic(TJDUI_TOPIC_EVENT_FIND_DEVICE);
MsgCenterSubscribeTopic(TJDUI_TOPIC_EVENT_ACTIVITY_RECOGNITION);
static uint8_t flag = 0;
if (flag == 0) {
flag = 1;
OHOS::NotificationManager::GetInstance()->SetNotifyPriority(TJD_UI_PRIO_LOWEST);
}
}
uint16_t TjdUiMsgCenterGetPriority(const Topic topic)
{
uint16_t mapperSize = sizeof(TJD_PRI_MAPPER) / sizeof(TJD_PRI_MAPPER[0]);
for (uint16_t i = 0; i < mapperSize; i++) {
if (topic == TJD_PRI_MAPPER[i].topicId) {
return TJD_PRI_MAPPER[i].prId;
}
}
return TJD_UI_PRIO_LOWEST;
}
void TjdUiMsgEventPublish(uint16 topic, void *data, uint16_t size)
{
int32 ret;
BroadcastApi *broadcastApi = nullptr;
IUnknown *api = nullptr;
const power_display_svr_api_t *display_api = power_display_svr_get_api();
if (display_api->get_screen_state() != SCREEN_ON) {
display_api->turn_on_screen();
}
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AlarmTimerEventPublish topic=%u!!", topic);
void *publishData = nullptr;
if (size != 0) {
publishData = malloc(size);
if (publishData == nullptr) {
printf("malloc failed, size:%d\n", size);
return;
}
ret = memcpy_s(publishData, size, (const void *)data, size);
if (ret != EOK) {
free(publishData);
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AlarmTimerEventPublish memcpy_s fail! ret = %d", ret);
return;
}
}
Request request = {
.msgId = topic,
.len = size,
.msgValue = 0,
.data = publishData,
};
/* public request */
api = SAMGR_GetInstance()->GetFeatureApi(BROADCAST_SERVICE_NAME, BROADCAST_FEATURE_NAME);
if (api == nullptr) {
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GetFeatureApi failed");
free(publishData);
return;
}
ret = api->QueryInterface(api, DEFAULT_VERSION, (void **)&broadcastApi);
if (ret != 0 || broadcastApi == nullptr) {
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "QueryInterface failed");
free(publishData);
return;
}
if (g_msgCenterEventCallback != nullptr) {
g_msgCenterEventCallback(topic);
}
broadcastApi->PublishTopic((IUnknown *)broadcastApi, &request);
broadcastApi->Release((IUnknown *)broadcastApi);
WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "AlarmTimerEventPublish end!!");
return;
}
void RegisterNotifyMsgCenterEventCallback(TjdUiMsgCenterEventCallback callback) { g_msgCenterEventCallback = callback; }
void UnRegisterNotifyMsgCenterEventCallback(void) { g_msgCenterEventCallback = nullptr; }