mcu_hi3321_watch/tjd/ui/app/phone/phone_app/TjdUiAppPhonePresenter.cpp
2025-05-26 20:15:20 +08:00

306 lines
9.7 KiB
C++

/*----------------------------------------------------------------------------
* Copyright (c) TJD Technologies Co., Ltd. 2025. All rights reserved.
*
* Description: TjdUiAppPhonePresenter.cpp
*
* Author: luziquan@ss-tjd.com
*
* Create: 2025-08-16
*--------------------------------------------------------------------------*/
#include "TjdUiAppPhonePresenter.h"
#include "NativeAbility.h"
#include "TjdPhoneContacts.h"
#include "TjdPhoneService.h"
#include "TjdUiAppCallModel.h"
#include "TjdUiAppIds.h"
#include "TjdUiAppPhoneModel.h"
#include "TjdUiAppPhoneView.h"
#include "TjdUiRegisterManager.h"
#include "dock/input_device.h"
#include "graphic_service.h"
#include "sys_config.h"
#include <cmsis_os.h>
#define ENABLE_PRINT_INFO 1
#if ENABLE_PRINT_INFO
#define static_print_info(...) sys_ui_log_i(__VA_ARGS__) // 一般信息打印宏控制
#define static_print_warn(...) sys_ui_log_w(__VA_ARGS__) // 警告信息打印一般常开
#define static_print_error(...) sys_ui_log_e(__VA_ARGS__) // 错误信息打印一般常开
#define static_print_debug(...) sys_ui_log_d(__VA_ARGS__) // 调试信息打印一般常开
#else
#define static_print_info(...)
#define static_print_warn(...)
#define static_print_error(...)
#define static_print_debug(...)
#endif
namespace TJD {
TJD_REGIST_NATIVE_MENU(TJD_APP_VIEW_PHONE, TjdUiAppPhoneView, TjdUiAppPhonePresenter, IMG_MENU_LIST_MENU_PHONE,
STR_ID_08);
static TjdUiAppPhonePresenter *g_phonePresenter = nullptr;
TjdUiAppPhonePresenter::TjdUiAppPhonePresenter() { g_phonePresenter = this; }
TjdUiAppPhonePresenter::~TjdUiAppPhonePresenter() { g_phonePresenter = nullptr; }
TjdUiAppPhonePresenter *TjdUiAppPhonePresenter::GetInstance(void) { return g_phonePresenter; }
void TjdUiAppPhonePresenter::OnStart()
{
TjdUiCommonOnKeyListener::GetInstance()->SetOnKeyActListener(this, KeyModelType::APP_KEY_TYPE);
view_->ShowView(TjdUiAppPhoneView::currentViewIndex_);
}
void TjdUiAppPhonePresenter::OnStop()
{
TjdUiCommonOnKeyListener::GetInstance()->ClearOnKeyActListener(KeyModelType::APP_KEY_TYPE);
OHOS::FocusManager::GetInstance()->ClearFocus();
}
void TjdUiAppPhonePresenter::ClearDialNumber()
{
dialNumber_.clear();
view_->SetDialNumber(dialNumber_);
}
void TjdUiAppPhonePresenter::UpdateCallLogList()
{
std::vector<CallLogInfo> infoVec = TjdUiAppPhoneModel::GetInstance().GetCallLogList();
callLogListCase_.clear();
int16_t index = 0;
for (const auto &info : infoVec) {
std::string name(info.name, info.nameLen);
std::string number(reinterpret_cast<const char *>(info.tel), info.telLen);
CallLogStatus status = static_cast<CallLogStatus>(info.status);
callLogListCase_.push_back({name, number, index++, status});
}
}
void TjdUiAppPhonePresenter::UpdateContactsList()
{
const std::vector<ContactInfo> &contacts = GetContacts();
contactListCase_.clear();
int index = 0;
for (const auto &contact : contacts) {
PhoneListCaseInfo info = {contact.name, contact.number, index++, CallLogStatus::CALL_LOG_STATUS_UNKNOWN};
contactListCase_.emplace_back(info);
}
}
void TjdUiAppPhonePresenter::ViewExitEvent(bool isSwipe)
{
/* 如果为快捷进入,则跳回上一个应用,而不是页面 */
if (TjdUiAppPhoneView::IsQuickEnter()) {
OHOS::NativeAbility::GetInstance().ChangePreSlice();
return;
}
switch (TjdUiAppPhoneView::currentViewIndex_) {
case PhoneViewIndex::PHONE_UNKNOWN:
case PhoneViewIndex::PHONE_MAIN_LIST:
OHOS::NativeAbility::GetInstance().ChangePreSlice();
break;
case PhoneViewIndex::PHONE_CONTACT:
case PhoneViewIndex::PHONE_CALL_LOG:
case PhoneViewIndex::PHONE_DIAL:
view_->ShowView(PhoneViewIndex::PHONE_MAIN_LIST);
break;
default:
break;
}
}
bool TjdUiAppPhonePresenter::OnKeyAct(OHOS::UIView &view, const OHOS::KeyEvent &event)
{
if (!TjdUiCommonOnKeyListener::CheckIsExitEvent(event)) {
return true;
}
ViewExitEvent(false);
return true;
}
void TjdUiAppPhonePresenter::ClickDialEvent() { view_->ShowView(PhoneViewIndex::PHONE_DIAL); }
void TjdUiAppPhonePresenter::ClickContactEvent()
{
UpdateContactsList();
view_->ShowView(PhoneViewIndex::PHONE_CONTACT);
}
void TjdUiAppPhonePresenter::ClickCallLogEvent()
{
UpdateCallLogList();
view_->ShowView(PhoneViewIndex::PHONE_CALL_LOG);
}
void TjdUiAppPhonePresenter::ClickDialKeyEvent(std::string key, bool isPopup)
{
if (isPopup) {
if (dialNumber_.empty()) {
return;
}
dialNumber_.pop_back();
if (key.empty()) {
view_->SetDialNumber(dialNumber_);
return;
}
dialNumber_.append(key);
} else {
if (dialNumber_.size() >= 20) {
return;
}
dialNumber_.append(key);
}
view_->SetDialNumber(dialNumber_);
}
void TjdUiAppPhonePresenter::ClickDialCallEvent(int index)
{
static_print_debug("index %d", index);
if (TjdUiAppPhoneModel::GetInstance().IsBtConnected() == false) {
view_->ShowView(PhoneViewIndex::PHONE_DISCONNECT);
StartJumpTimer(index == -1 ? PhoneViewIndex::PHONE_DIAL : PhoneViewIndex::PHONE_CONTACT);
return;
}
std::string showCallingTest;
std::string dialCallNumber;
if (index == -1) {
if (dialNumber_.size() == 0) {
return;
}
/* 电话播出需要寻找一下通讯录 */
const ContactInfo *contact = FindContactOfNumber(reinterpret_cast<const unsigned char *>(dialNumber_.c_str()),
static_cast<unsigned char>(dialNumber_.size()));
showCallingTest = contact ? contact->name : dialNumber_;
dialCallNumber = (dialNumber_);
} else {
/* index != -1为点击通讯录拨打电话 */
auto it = contactListCase_.begin();
std::advance(it, index);
showCallingTest = it->name;
dialCallNumber = (it->number);
}
static_print_debug("showCallingTest %s, dialCallNumber %s", showCallingTest.c_str(), dialCallNumber.c_str());
TjdUiAppCallModel::GetInstance().SetShowViewType(ShowCallViewType::CALL_OUT_VIEW);
TjdUiAppCallModel::GetInstance().SetCallingTest(showCallingTest);
TjdUiAppPhoneModel::GetInstance().DialCall(dialCallNumber);
OHOS::NativeAbility::GetInstance().ChangeSlice(TJD_APP_VIEW_CALL);
SetNeedRefreshCallLog(true);
}
void TjdUiAppPhonePresenter::StartJumpTimer(PhoneViewIndex jumpView)
{
if (jumpTimerId_ == nullptr) {
jumpTimerId_ = osTimerNew(
[](void *arg) {
PhoneViewIndex jumpView = static_cast<PhoneViewIndex>(reinterpret_cast<intptr_t>(arg));
GraphicService::GetInstance()->PostGraphicEvent([jumpView]() {
TjdUiAppPhoneView *view = TjdUiAppPhoneView::GetInstance();
TjdUiAppPhonePresenter *presenter = TjdUiAppPhonePresenter::GetInstance();
if (view == nullptr) {
return;
}
view->ShowView(jumpView);
presenter->DeleteJumpTimer();
});
},
osTimerOnce, reinterpret_cast<void *>(static_cast<intptr_t>(jumpView)), nullptr);
if (jumpTimerId_ == nullptr) {
static_print_error("TjdUiAppPhonePresenter::StartJumpTimer Create timer fail.");
return;
}
}
int32_t ret = osTimerStart(jumpTimerId_, 2000);
if (ret != 0) {
static_print_error("TjdUiAppPhonePresenter::StartJumpTimer Start timer fail");
return;
}
}
void TjdUiAppPhonePresenter::DeleteJumpTimer()
{
if (jumpTimerId_ != nullptr) {
osTimerDelete(jumpTimerId_);
jumpTimerId_ = nullptr;
}
}
struct OnClickMapper
{
const char *id;
void (TjdUiAppPhonePresenter::*func)();
};
static const OnClickMapper g_onClickMapper[] = {
{BTN_DIAL_VIEW_ID, &TjdUiAppPhonePresenter::ClickDialEvent}, // 进入拨号界面
{BTN_CONTACT_VIEW_ID, &TjdUiAppPhonePresenter::ClickContactEvent}, // 进入通讯录界面
{BTN_CALL_LOG_VIEW_ID, &TjdUiAppPhonePresenter::ClickCallLogEvent}, // 进入通话记录界面
};
bool TjdUiAppPhonePresenter::OnClick(OHOS::UIView &view, const OHOS::ClickEvent &event)
{
std::string viewId = view.GetViewId();
if (viewId.empty()) {
return false;
}
for (const auto &item : g_onClickMapper) {
if (strcmp(viewId.c_str(), item.id) == 0) {
(GetInstance()->*item.func)();
return true;
}
}
return false;
}
bool TjdUiAppPhonePresenter::OnPress(OHOS::UIView &view, const OHOS::PressEvent &event)
{
std::string viewId = view.GetViewId();
if (viewId.empty()) {
return false;
}
if (viewId != "+" && viewId != "-") {
ClickDialKeyEvent(viewId, false);
}
return false;
}
bool TjdUiAppPhonePresenter::OnRelease(OHOS::UIView &view, const OHOS::ReleaseEvent &event)
{
std::string viewId = view.GetViewId();
if (viewId.empty()) {
return false;
}
if (viewId == "+") { // 拨号
ClickDialCallEvent(-1);
} else if (viewId == "-") { // 删除
ClickDialKeyEvent("", true);
}
return false;
}
bool TjdUiAppPhonePresenter::OnLongPress(OHOS::UIView &view, const OHOS::LongPressEvent &event)
{
std::string viewId = view.GetViewId();
if (viewId.empty()) {
return false;
}
if (viewId == "+") {
return false;
} else if (viewId == "-") {
ClearDialNumber();
} else if (viewId == "7") {
ClickDialKeyEvent("*", true);
} else if (viewId == "9") {
ClickDialKeyEvent("#", true);
} else if (viewId == "0") {
ClickDialKeyEvent("+", true);
}
return false;
}
} // namespace TJD