Files
MuditaOS/module-apps/application-call/windows/CallWindow.cpp
2019-08-06 23:23:05 +02:00

364 lines
12 KiB
C++

/*
* @file CallWindow.cpp
* @author Robert Borzecki (robert.borzecki@mudita.com)
* @date 12 lip 2019
* @brief
* @copyright Copyright (C) 2019 mudita.com
* @details
*/
#include <memory>
#include <functional>
#include <sstream>
#include <iomanip>
#include "service-appmgr/ApplicationManager.hpp"
#include "../ApplicationCall.hpp"
#include "../data/CallSwitchData.hpp"
#include "service-audio/api/AudioServiceAPI.hpp"
#include "i18/i18.hpp"
#include "service-cellular/api/CellularServiceAPI.hpp"
#include "Label.hpp"
#include "Margins.hpp"
#include "CallWindow.hpp"
namespace gui {
CallWindow::CallWindow( app::Application* app, std::string windowName ) : AppWindow(app, windowName){
setSize( 480, 600 );
buildInterface();
}
void CallWindow::rebuild() {
destroyInterface();
buildInterface();
}
void CallWindow::buildInterface() {
AppWindow::buildInterface();
bottomBar->setActive( BottomBar::Side::CENTER, true );
bottomBar->setActive( BottomBar::Side::RIGHT, true );
bottomBar->setText( BottomBar::Side::CENTER, utils::localize.get("common_select"));
bottomBar->setText( BottomBar::Side::RIGHT, utils::localize.get("common_back"));
titleLabel = new gui::Label(this, 0, 50, 480, 50 );
titleLabel->setFilled( false );
titleLabel->setBorderColor( gui::ColorNoColor );
titleLabel->setFont("gt_pressura_bold_24");
titleLabel->setAlignement( gui::Alignment(gui::Alignment::ALIGN_HORIZONTAL_CENTER, gui::Alignment::ALIGN_VERTICAL_CENTER));
durationLabel = new gui::Label(this, 0, 270, 480, 50 );
durationLabel->setFilled( false );
durationLabel->setBorderColor( gui::ColorNoColor );
durationLabel->setFont("gt_pressura_regular_24");
durationLabel->setAlignement( gui::Alignment(gui::Alignment::ALIGN_HORIZONTAL_CENTER, gui::Alignment::ALIGN_VERTICAL_BOTTOM));
numberLabel = new gui::Label(this, 0, 150, 480, 50 );
numberLabel->setFilled( false );
numberLabel->setBorderColor( gui::ColorNoColor );
numberLabel->setFont("gt_pressura_bold_24");
numberLabel->setAlignement( gui::Alignment(gui::Alignment::ALIGN_HORIZONTAL_CENTER, gui::Alignment::ALIGN_VERTICAL_CENTER));
//create circles to hold images inside
for( uint32_t i=0; i<3; ++i ) {
rects[i] = new gui::Rect( this, 0,0, 80, 80 );
rects[i]->setFilled( false );
rects[i]->setRadius(40);
rects[i]->setPenFocusWidth(3);
rects[i]->setPenWidth(1);
}
rects[static_cast<uint32_t>(FocusRects::Messages)]->setPosition( 200, 400 );
rects[static_cast<uint32_t>(FocusRects::Speaker)]->setPosition( 150, 400 );
rects[static_cast<uint32_t>(FocusRects::Micrphone)]->setPosition( 250, 400 );
rects[static_cast<uint32_t>(FocusRects::Messages)]->setPenWidth(3);
imageSpeaker[static_cast<uint32_t>(AudioState::ON)] = new gui::Image( rects[static_cast<uint32_t>(FocusRects::Speaker)], 20, 20, 0,0, "speaker_on" );
imageSpeaker[static_cast<uint32_t>(AudioState::OFF)] = new gui::Image( rects[static_cast<uint32_t>(FocusRects::Speaker)], 20, 20, 0,0, "speaker_off" );
imageSpeaker[static_cast<uint32_t>(AudioState::ON)]->setVisible(false);
imageSpeaker[static_cast<uint32_t>(AudioState::OFF)]->setVisible(false);
imageMessage = new gui::Image( rects[static_cast<uint32_t>(FocusRects::Messages)], 15, 15, 0,0, "menu_messages" );
imageMicrophone[static_cast<uint32_t>(AudioState::ON)] = new gui::Image( rects[static_cast<uint32_t>(FocusRects::Micrphone)], 20, 20, 0,0, "microphone_on" );
imageMicrophone[static_cast<uint32_t>(AudioState::OFF)] = new gui::Image( rects[static_cast<uint32_t>(FocusRects::Micrphone)], 20, 20, 0,0, "microphone_off" );
imageMicrophone[static_cast<uint32_t>(AudioState::ON)]->setVisible(false);
imageMicrophone[static_cast<uint32_t>(AudioState::OFF)]->setVisible(false);
//define navigation between labels
rects[static_cast<uint32_t>(FocusRects::Speaker)]->setNavigationItem( NavigationDirection::LEFT,
rects[static_cast<uint32_t>(FocusRects::Micrphone)]);
rects[static_cast<uint32_t>(FocusRects::Speaker)]->setNavigationItem( NavigationDirection::RIGHT,
rects[static_cast<uint32_t>(FocusRects::Micrphone)]);
rects[static_cast<uint32_t>(FocusRects::Micrphone)]->setNavigationItem( NavigationDirection::LEFT,
rects[static_cast<uint32_t>(FocusRects::Speaker)]);
rects[static_cast<uint32_t>(FocusRects::Micrphone)]->setNavigationItem( NavigationDirection::RIGHT,
rects[static_cast<uint32_t>(FocusRects::Speaker)]);
//focus callbacks
rects[static_cast<uint32_t>(FocusRects::Speaker)]->focusChangedCallback = [=] (gui::Item& item){
LOG_INFO("Speaker gets focus" );
bottomBar->setText( BottomBar::Side::CENTER, utils::localize.get("common_speaker"));
return true; };
rects[static_cast<uint32_t>(FocusRects::Micrphone)]->focusChangedCallback = [=] (gui::Item& item){
LOG_INFO("Mute gets focus" );
bottomBar->setText( BottomBar::Side::CENTER, utils::localize.get("common_mute"));
return true; };
//activation callbacks
rects[static_cast<uint32_t>(FocusRects::Speaker)]->activatedCallback = [=] (gui::Item& item){
LOG_INFO("Speaker activated" );
//update icon
imageSpeaker[static_cast<uint32_t>(speakerState)]->setVisible(false);
speakerState = (speakerState == AudioState::ON)?AudioState::OFF:AudioState::ON;
imageSpeaker[static_cast<uint32_t>(speakerState)]->setVisible(true);
application->refreshWindow( RefreshModes::GUI_REFRESH_FAST );
(speakerState == AudioState::ON)?
AudioServiceAPI::RoutingSpeakerPhone(this->application,true):
AudioServiceAPI::RoutingSpeakerPhone(this->application,false);
return true; };
rects[static_cast<uint32_t>(FocusRects::Micrphone)]->activatedCallback = [=] (gui::Item& item){
LOG_INFO("Mute activated" );
//update icon
imageMicrophone[static_cast<uint32_t>(microphoneState)]->setVisible(false);
microphoneState = (microphoneState == AudioState::ON)?AudioState::OFF:AudioState::ON;
imageMicrophone[static_cast<uint32_t>(microphoneState)]->setVisible(true);
application->refreshWindow( RefreshModes::GUI_REFRESH_FAST );
(microphoneState == AudioState::ON)?
AudioServiceAPI::RoutingMute(this->application,true):
AudioServiceAPI::RoutingMute(this->application,false);
return true; };
bottomBar->setText( gui::BottomBar::Side::CENTER, "Message" );
}
void CallWindow::destroyInterface() {
AppWindow::destroyInterface();
delete titleLabel;
delete numberLabel;
children.remove( titleLabel );
children.remove( numberLabel );
}
CallWindow::~CallWindow() {
}
void CallWindow::setState( State state ) {
this->state = state;
setVisibleState();
}
void CallWindow::setVisibleState() {
rects[static_cast<uint32_t>(FocusRects::Speaker)]->setVisible(false);
rects[static_cast<uint32_t>(FocusRects::Messages)]->setVisible(false);
rects[static_cast<uint32_t>(FocusRects::Micrphone)]->setVisible(false);
durationLabel->setVisible(false);
//show state of the window
switch( state ) {
case State::INCOMMING_CALL: {
titleLabel->setText("INCOMMING_CALL");
bottomBar->setActive(gui::BottomBar::Side::LEFT, true );
bottomBar->setActive(gui::BottomBar::Side::CENTER, true );
bottomBar->setActive(gui::BottomBar::Side::RIGHT, true );
bottomBar->setText( gui::BottomBar::Side::LEFT, "Accept" );
bottomBar->setText( gui::BottomBar::Side::CENTER, "Message" );
bottomBar->setText( gui::BottomBar::Side::RIGHT, "Reject" );
rects[static_cast<uint32_t>(FocusRects::Messages)]->setVisible(true);
}break;
case State::CALL_ENDED: {
titleLabel->setText("CALL_ENDED");
bottomBar->setActive(gui::BottomBar::Side::LEFT, false );
bottomBar->setActive(gui::BottomBar::Side::CENTER, false );
bottomBar->setActive(gui::BottomBar::Side::RIGHT, true );
bottomBar->setText( gui::BottomBar::Side::RIGHT, "Return" );
}break;
case State::CALL_IN_PROGRESS: {
titleLabel->setText("CALL_IN_PROGRESS");
durationLabel->setVisible(true);
bottomBar->setActive(gui::BottomBar::Side::LEFT, false );
bottomBar->setActive(gui::BottomBar::Side::CENTER, false );
bottomBar->setActive(gui::BottomBar::Side::RIGHT, true );
bottomBar->setText( gui::BottomBar::Side::RIGHT, "End Call" );
rects[static_cast<uint32_t>(FocusRects::Speaker)]->setVisible(true);
rects[static_cast<uint32_t>(FocusRects::Micrphone)]->setVisible(true);
imageSpeaker[static_cast<uint32_t>(speakerState)]->setVisible(true);
imageMicrophone[static_cast<uint32_t>(microphoneState)]->setVisible(true);
setFocusItem( rects[static_cast<uint32_t>(FocusRects::Speaker)] );
}break;
case State::IDLE: {
titleLabel->setText("IDLE");
}break;
case State::OUTGOING_CALL: {
titleLabel->setText("OUTGOING_CALL");
bottomBar->setActive(gui::BottomBar::Side::LEFT, false );
bottomBar->setActive(gui::BottomBar::Side::CENTER, false );
bottomBar->setActive(gui::BottomBar::Side::RIGHT, true );
bottomBar->setText( gui::BottomBar::Side::RIGHT, "End Call" );
}break;
};
}
void CallWindow::updateDuration( uint32_t duration ) {
uint32_t seconds = 0;
uint32_t minutes = 0;
uint32_t hours = 0;
uint32_t days = 0;
days = duration / 86400; duration -= days*86400;
hours = duration / 3600; duration -= hours*3600;
minutes = duration / 60; duration -= minutes*60;
seconds = duration;
std::stringstream ss;
if( days ) ss<<days<<":";
if( hours ) ss<<hours<<":";
if( hours && minutes<10) ss << "0";
ss<<minutes << ":";
ss<<std::setfill('0') << std::setw(2) << seconds;
durationLabel->setText( ss.str());
}
bool CallWindow::handleSwitchData( SwitchData* data ) {
if( data == nullptr )
LOG_ERROR("Received null pointer");
return false;
app::CallSwitchData* callData = reinterpret_cast<app::CallSwitchData*>(data);
if( callData->getType() == app::CallSwitchData::Type::INCOMMING_CALL ) {
app::IncommingCallData* incData = reinterpret_cast<app::IncommingCallData*>( data );
state = State::INCOMMING_CALL;
numberLabel->setText( incData->getPhoneNumber());
}
setVisibleState();
application->refreshWindow( RefreshModes::GUI_REFRESH_FAST );
return true;
}
void CallWindow::onBeforeShow( ShowMode mode, uint32_t command, SwitchData* data ) {
AudioServiceAPI::RoutingSpeakerPhone(this->application,false);
AudioServiceAPI::RoutingMute(this->application,false);
bottomBar->setText( BottomBar::Side::CENTER, utils::localize.get("common_speaker"));
}
bool CallWindow::handleLeftButton() {
if( state == State::INCOMMING_CALL ) {
auto ret = CellularServiceAPI::AnswerIncomingCall(application);
LOG_INFO("AnswerIncomingCall: %s",(ret?"OK":"FAIL"));
return true;
}
else if( state == State::OUTGOING_CALL ) {
}
else if( state == State::CALL_ENDED ) {
}
else if( state == State::CALL_IN_PROGRESS ) {
}
return false;
}
bool CallWindow::handleCenterButton() {
if( state == State::INCOMMING_CALL ) {
auto ret = CellularServiceAPI::HangupCall(application);
LOG_INFO("HangupCall: %s",(ret?"OK":"FAIL"));
//TODO switch to message templates window
return true;
}
else if( state == State::OUTGOING_CALL ) {
}
else if( state == State::CALL_ENDED ) {
}
else if( state == State::CALL_IN_PROGRESS ) {
}
return false;
}
bool CallWindow::handleRightButton() {
if( state == State::INCOMMING_CALL ) {
auto ret = CellularServiceAPI::HangupCall(application);
LOG_INFO("HangupCall: %s",(ret?"OK":"FAIL"));
return true;
}
else if( state == State::OUTGOING_CALL ) {
auto ret = CellularServiceAPI::HangupCall(application);
LOG_INFO("HangupCall: %s",(ret?"OK":"FAIL"));
}
else if( state == State::CALL_ENDED ) {
//return to previous application
sapm::ApplicationManager::messageSwitchPreviousApplication( application );
return true;
}
else if( state == State::CALL_IN_PROGRESS ) {
auto ret = CellularServiceAPI::HangupCall(application);
LOG_INFO("HangupCall: %s",(ret?"OK":"FAIL"));
return true;
}
return false;
}
bool CallWindow::onInput( const InputEvent& inputEvent ) {
//check if any of the lower inheritance onInput methods catch the event
bool ret = AppWindow::onInput( inputEvent );
if( ret ) {
//refresh window only when key is other than enter
if( inputEvent.keyCode != KeyCode::KEY_ENTER )
application->render( RefreshModes::GUI_REFRESH_FAST );
return true;
}
bool handled = false;
//process only if key is released
if( inputEvent.state != InputEvent::State::keyReleasedShort ) {
if( inputEvent.keyCode == KeyCode::KEY_ENTER ) {
handled = handleCenterButton();
}
else if( inputEvent.keyCode == KeyCode::KEY_LF ) {
handled = handleLeftButton();
}
else if( inputEvent.keyCode == KeyCode::KEY_RF ) {
handled = handleRightButton();
}
}
if( handled )
application->refreshWindow( RefreshModes::GUI_REFRESH_FAST);
return false;
}
} /* namespace gui */