mirror of
https://github.com/mudita/MuditaOS.git
synced 2026-01-24 13:58:00 -05:00
* Moved ApplicationLauncher to separate files - declutering * WindowsStore renamed to WindowsFactory - Store needs to be splitted toavoid antipattern * Options window build dynamically onBeforeShow * Dialog windows dynamically built
289 lines
10 KiB
C++
289 lines
10 KiB
C++
#include "ApplicationCall.hpp"
|
|
|
|
#include "DialogMetadata.hpp"
|
|
#include "DialogMetadataMessage.hpp"
|
|
#include "data/CallSwitchData.hpp"
|
|
#include "windows/CallMainWindow.hpp"
|
|
#include "windows/CallWindow.hpp"
|
|
#include "windows/EmergencyCallWindow.hpp"
|
|
#include "windows/EnterNumberWindow.hpp"
|
|
|
|
#include <Application.hpp>
|
|
#include <MessageType.hpp>
|
|
#include <PhoneNumber.hpp>
|
|
#include <UiCommonActions.hpp>
|
|
#include <Dialog.hpp>
|
|
#include <log/log.hpp>
|
|
#include <service-cellular/ServiceCellular.hpp>
|
|
#include <service-cellular/api/CellularServiceAPI.hpp>
|
|
#include <service-audio/api/AudioServiceAPI.hpp>
|
|
#include <service-appmgr/ApplicationManager.hpp>
|
|
#include <time/time_conversion.hpp>
|
|
#include <ticks.hpp>
|
|
|
|
#include <cassert>
|
|
|
|
namespace app
|
|
{
|
|
|
|
ApplicationCall::ApplicationCall(std::string name, std::string parent, bool startBackground)
|
|
: Application(name, parent, startBackground, app::call_stack_size)
|
|
{
|
|
timerCall = std::make_unique<sys::Timer>("Call", this, 1000);
|
|
timerCall->connect([this](sys::Timer &) { timerCallCallback(); });
|
|
}
|
|
|
|
// number of seconds after end call to switch back to previous application
|
|
const inline utils::time::Duration delayToSwitchToPreviousApp = 3;
|
|
|
|
void ApplicationCall::timerCallCallback()
|
|
{
|
|
callDuration = utils::time::Timestamp() - callStartTime;
|
|
|
|
if (getCurrentWindow() == windowsStack.get(window::name_call)) {
|
|
auto callWindow = dynamic_cast<gui::CallWindow *>(getCurrentWindow());
|
|
|
|
if (callWindow != nullptr && callWindow->getState() == gui::CallWindow::State::CALL_IN_PROGRESS) {
|
|
callWindow->updateDuration(callDuration);
|
|
refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ApplicationCall::CallAbortHandler()
|
|
{
|
|
auto callWindow = dynamic_cast<gui::CallWindow *>(windowsStack.get(window::name_call));
|
|
assert(callWindow != nullptr);
|
|
|
|
LOG_INFO("---------------------------------CallAborted");
|
|
AudioServiceAPI::StopAll(this);
|
|
|
|
callWindow->setState(gui::CallWindow::State::CALL_ENDED);
|
|
if (getState() == State::ACTIVE_FORGROUND && getCurrentWindow() != callWindow) {
|
|
switchWindow(window::name_call);
|
|
return;
|
|
}
|
|
else if (getState() == State::ACTIVE_BACKGROUND) {
|
|
// it means we have switched to different application during call and the call was aborted
|
|
// hence we need to switch back to call application
|
|
sapm::ApplicationManager::messageSwitchPreviousApplication(this);
|
|
}
|
|
|
|
refreshWindow(gui::RefreshModes::GUI_REFRESH_DEEP);
|
|
}
|
|
|
|
void ApplicationCall::CallActiveHandler()
|
|
{
|
|
auto callWindow = dynamic_cast<gui::CallWindow *>(windowsStack.get(window::name_call));
|
|
assert(callWindow != nullptr);
|
|
|
|
AudioServiceAPI::RoutingStart(this);
|
|
runCallTimer();
|
|
|
|
LOG_INFO("---------------------------------CallActive");
|
|
callWindow->setState(gui::CallWindow::State::CALL_IN_PROGRESS);
|
|
refreshWindow(gui::RefreshModes::GUI_REFRESH_DEEP);
|
|
}
|
|
|
|
void ApplicationCall::IncomingCallHandler(const CellularCallMessage *const msg)
|
|
{
|
|
auto callWindow = dynamic_cast<gui::CallWindow *>(windowsStack.get(window::name_call));
|
|
assert(callWindow != nullptr);
|
|
|
|
LOG_INFO("---------------------------------IncomingCall");
|
|
if (callWindow->getState() == gui::CallWindow::State::INCOMING_CALL) {
|
|
LOG_INFO("ignoring call incoming");
|
|
}
|
|
else {
|
|
AudioServiceAPI::PlaybackStart(this, audio::PlaybackType::CallRingtone, ringtone_path);
|
|
runCallTimer();
|
|
std::unique_ptr<gui::SwitchData> data = std::make_unique<app::IncomingCallData>(msg->number);
|
|
// send to itself message to switch (run) call application
|
|
callWindow->setState(gui::CallWindow::State::INCOMING_CALL);
|
|
if (getState() == State::ACTIVE_FORGROUND) {
|
|
LOG_INFO("++++++++++++WINDOW SWITCH");
|
|
switchWindow(window::name_call, std::move(data));
|
|
}
|
|
else {
|
|
LOG_INFO("++++++++++++APP SWITCH");
|
|
|
|
sapm::ApplicationManager::messageSwitchApplication(this, name_call, window::name_call, std::move(data));
|
|
}
|
|
}
|
|
}
|
|
|
|
void ApplicationCall::RingingHandler(const CellularCallMessage *const msg)
|
|
{
|
|
auto callWindow = dynamic_cast<gui::CallWindow *>(windowsStack.get(window::name_call));
|
|
assert(callWindow != nullptr);
|
|
|
|
LOG_INFO("---------------------------------Ringing");
|
|
AudioServiceAPI::RoutingStart(this);
|
|
runCallTimer();
|
|
|
|
std::unique_ptr<gui::SwitchData> data = std::make_unique<app::ExecuteCallData>(msg->number);
|
|
callWindow->setState(gui::CallWindow::State::OUTGOING_CALL);
|
|
if (getState() == State::ACTIVE_FORGROUND) {
|
|
switchWindow(window::name_call, std::move(data));
|
|
}
|
|
}
|
|
|
|
// Invoked upon receiving data message
|
|
sys::Message_t ApplicationCall::DataReceivedHandler(sys::DataMessage *msgl, sys::ResponseMessage *resp)
|
|
{
|
|
|
|
auto retMsg = Application::DataReceivedHandler(msgl);
|
|
// if message was handled by application's template there is no need to process further.
|
|
auto response = dynamic_cast<sys::ResponseMessage *>(retMsg.get());
|
|
assert(response);
|
|
if (response->retCode == sys::ReturnCodes::Success) {
|
|
return retMsg;
|
|
}
|
|
|
|
if (msgl->messageType == MessageType::CellularNotification) {
|
|
CellularNotificationMessage *msg = dynamic_cast<CellularNotificationMessage *>(msgl);
|
|
assert(msg != nullptr);
|
|
|
|
switch (msg->type) {
|
|
case CellularNotificationMessage::Type::CallAborted: {
|
|
CallAbortHandler();
|
|
} break;
|
|
case CellularNotificationMessage::Type::CallActive: {
|
|
CallActiveHandler();
|
|
} break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return std::make_shared<sys::ResponseMessage>();
|
|
}
|
|
else if (msgl->messageType == MessageType::CellularCall) {
|
|
auto *msg = dynamic_cast<CellularCallMessage *>(msgl);
|
|
assert(msg != nullptr);
|
|
|
|
switch (msg->type) {
|
|
case CellularCallMessage::Type::Ringing: {
|
|
RingingHandler(msg);
|
|
} break;
|
|
case CellularCallMessage::Type::IncomingCall: {
|
|
IncomingCallHandler(msg);
|
|
} break;
|
|
}
|
|
}
|
|
|
|
if (resp != nullptr) {
|
|
switch (resp->responseTo) {
|
|
case MessageType::CellularHangupCall: {
|
|
if (resp->retCode == sys::ReturnCodes::Success) {
|
|
CallAbortHandler();
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return std::make_shared<sys::ResponseMessage>();
|
|
}
|
|
|
|
return std::make_shared<sys::ResponseMessage>(sys::ReturnCodes::Unresolved);
|
|
}
|
|
|
|
// Invoked during initialization
|
|
sys::ReturnCodes ApplicationCall::InitHandler()
|
|
{
|
|
|
|
auto ret = Application::InitHandler();
|
|
if (ret != sys::ReturnCodes::Success)
|
|
return ret;
|
|
|
|
createUserInterface();
|
|
|
|
setActiveWindow("MainWindow");
|
|
|
|
return ret;
|
|
}
|
|
|
|
sys::ReturnCodes ApplicationCall::DeinitHandler()
|
|
{
|
|
return sys::ReturnCodes::Success;
|
|
}
|
|
|
|
void ApplicationCall::createUserInterface()
|
|
{
|
|
windowsFactory.attach(gui::name::window::main_window, [](Application *app, const std::string name) {
|
|
return std::make_unique<gui::CallMainWindow>(app);
|
|
});
|
|
windowsFactory.attach(app::window::name_enterNumber, [](Application *app, const std::string newname) {
|
|
return std::make_unique<gui::EnterNumberWindow>(app);
|
|
});
|
|
windowsFactory.attach(app::window::name_call, [](Application *app, const std::string &name) {
|
|
return std::make_unique<gui::CallWindow>(app);
|
|
});
|
|
windowsFactory.attach(app::window::name_emergencyCall, [](Application *app, const std::string &name) {
|
|
return std::make_unique<gui::EmergencyCallWindow>(app);
|
|
});
|
|
windowsFactory.attach(app::window::name_dialogConfirm, [](Application *app, const std::string &name) {
|
|
return std::make_unique<gui::DialogConfirm>(app, name);
|
|
});
|
|
}
|
|
|
|
bool ApplicationCall::showNotification(std::function<bool()> action)
|
|
{
|
|
gui::DialogMetadata meta;
|
|
meta.icon = "info_big_circle_W_G";
|
|
meta.text = utils::localize.get("app_call_no_sim");
|
|
meta.action = action;
|
|
switchWindow(app::window::name_dialogConfirm, std::make_unique<gui::DialogMetadataMessage>(meta));
|
|
return true;
|
|
}
|
|
|
|
void ApplicationCall::runCallTimer()
|
|
{
|
|
callStartTime = utils::time::Timestamp();
|
|
callDuration = 0;
|
|
timerCall->reload();
|
|
}
|
|
|
|
void ApplicationCall::stopCallTimer()
|
|
{
|
|
callStartTime = 0;
|
|
callDuration = 0;
|
|
timerCall->stop();
|
|
}
|
|
|
|
void ApplicationCall::destroyUserInterface()
|
|
{}
|
|
|
|
void ApplicationCall::handleCallEvent(const std::string &number)
|
|
{
|
|
if (!Store::GSM::get()->simCardInserted()) {
|
|
LOG_INFO("No SIM card");
|
|
auto action = [=]() -> bool {
|
|
returnToPreviousWindow();
|
|
return true;
|
|
};
|
|
showNotification(action);
|
|
return;
|
|
}
|
|
|
|
LOG_INFO("number: [%s]", number.c_str());
|
|
auto ret = CellularServiceAPI::DialNumber(this, utils::PhoneNumber(number));
|
|
LOG_INFO("CALL RESULT: %s", (ret ? "OK" : "FAIL"));
|
|
}
|
|
|
|
void ApplicationCall::handleAddContactEvent(const std::string &number)
|
|
{
|
|
LOG_INFO("add contact information: %s", number.c_str());
|
|
app::contact(this, app::ContactOperation::Add, number);
|
|
}
|
|
|
|
void ApplicationCall::transmitDtmfTone(uint32_t digit)
|
|
{
|
|
if (!CellularServiceAPI::TransmitDtmfTones(this, digit)) {
|
|
LOG_ERROR("transmitDtmfTone failed");
|
|
}
|
|
}
|
|
|
|
} // namespace app
|