~aleteoryx/muditaos

ref: c1391090c67428aeaffcef5a7a71a6d2ed69ccc9 muditaos/module-apps/apps-common/notifications/NotificationsHandler.cpp -rw-r--r-- 3.6 KiB
c1391090 — Mateusz Piesta [BH-1389] Catch2 unit tests optimization 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "NotificationsHandler.hpp"
#include <service-db/DBServiceAPI.hpp>
#include <log/log.hpp>
#include <service-cellular/service-cellular/CellularMessage.hpp>
#include <service-appmgr/Controller.hpp>
#include <service-audio/AudioServiceAPI.hpp>
#include <service-cellular/CellularServiceAPI.hpp>

using namespace notifications;

NotificationsHandler::NotificationsHandler(sys::Service *parentService, NotificationsConfiguration &notifcationConfig)
    : parentService{parentService}, notifcationConfig{notifcationConfig}
{}

void NotificationsHandler::registerMessageHandlers()
{
    parentService->connect(typeid(CellularIncominCallMessage), [&](sys::Message *request) -> sys::MessagePointer {
        incomingCallHandler(request);
        return sys::msgHandled();
    });

    parentService->connect(typeid(CellularCallerIdMessage), [&](sys::Message *request) -> sys::MessagePointer {
        callerIdHandler(request);
        return sys::msgHandled();
    });

    parentService->connect(typeid(CellularIncomingSMSNotificationMessage),
                           [&](sys::Message *request) -> sys::MessagePointer {
                               incomingSMSHandler();
                               return sys::msgHandled();
                           });
}

void NotificationsHandler::incomingCallHandler(sys::Message *request)
{
    notifcationConfig.updateCurrentCall(currentCallPolicy);

    if (currentCallPolicy.isPopupAllowed()) {
        auto msg = static_cast<CellularIncominCallMessage *>(request);
        app::manager::Controller::sendAction(parentService,
                                             app::manager::actions::HandleIncomingCall,
                                             std::make_unique<app::manager::actions::CallParams>(msg->number));
        playbackCallRingtone();
    }
}

void NotificationsHandler::callerIdHandler(sys::Message *request)
{
    auto msg = static_cast<CellularCallerIdMessage *>(request);

    if (currentCallPolicy.isNumberCheckRequired()) {
        policyNumberCheck(msg->number);
    }
    if (currentCallPolicy.isPopupAllowed()) {
        app::manager::Controller::sendAction(parentService,
                                             app::manager::actions::HandleCallerId,
                                             std::make_unique<app::manager::actions::CallParams>(msg->number));
        playbackCallRingtone();
    }
    else {
        CellularServiceAPI::DismissCall(parentService, currentCallPolicy.isDismissedCallNotificationAllowed());
    }
}

void NotificationsHandler::policyNumberCheck(const utils::PhoneNumber::View &number)
{
    auto isContactInFavourites = DBServiceAPI::IsContactInFavourites(parentService, number);
    notifcationConfig.callNumberCheck(currentCallPolicy, isContactInFavourites);
}

void NotificationsHandler::incomingSMSHandler()
{
    notifcationConfig.updateCurrentSMS(currentSMSPolicy);
    playbackSMSRingtone();
}

void NotificationsHandler::playbackCallRingtone()
{
    if (currentCallPolicy.isRingtoneAllowed()) {
        const auto filePath = AudioServiceAPI::GetSound(parentService, audio::PlaybackType::CallRingtone);
        AudioServiceAPI::PlaybackStart(parentService, audio::PlaybackType::CallRingtone, filePath);
    }
}

void NotificationsHandler::playbackSMSRingtone()
{
    if (currentSMSPolicy.isRingtoneAllowed()) {
        const auto filePath = AudioServiceAPI::GetSound(parentService, audio::PlaybackType::TextMessageRingtone);
        AudioServiceAPI::PlaybackStart(parentService, audio::PlaybackType::TextMessageRingtone, filePath);
    }
}