~aleteoryx/muditaos

ref: 040550f3b72f2f2d57c2171b963da301ef00cf4f muditaos/module-apps/notifications/NotificationProvider.cpp -rw-r--r-- 4.2 KiB
040550f3 — Pawel.Paprocki [BH-356] Move TPLIB (microtar) to separate directory 4 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "NotificationProvider.hpp"
#include <Service/Service.hpp>
#include <module-db/queries/notifications/QueryNotificationsGetAll.hpp>
#include <service-appmgr/Controller.hpp>
#include <service-appmgr/data/NotificationsChangedActionsParams.hpp>
#include <service-db/DBNotificationMessage.hpp>
#include <NotificationsRecord.hpp>

using namespace notifications;

NotificationProvider::NotificationProvider(sys::Service *ownerService) : ownerService{ownerService}
{}

template <NotificationType type, typename T>
bool NotificationProvider::handleNotSeenWithCounter(NotificationsRecord &&record)
{
    auto value = record.value;
    if (notifications.count(type) > 0) {
        if (value == 0) {
            notifications.erase(type);
            return true;
        }
        if (auto notification = static_cast<T *>(notifications[type].get()); value > notification->getValue()) {
            notifications[type] = std::make_shared<T>(value, std::move(record.contactRecord));
            return true;
        }
    }
    else if (value > 0) {
        notifications[type] = std::make_shared<T>(value, std::move(record.contactRecord));
        return true;
    }
    return false;
}

void NotificationProvider::handle(db::query::notifications::GetAllResult *msg)
{
    assert(msg);
    auto records = *msg->getResult();

    bool notificationsChanged = false;
    for (auto &&record : records) {
        switch (record.key) {
        case NotificationsRecord::Key::Calls:
            notificationsChanged |=
                handleNotSeenWithCounter<NotificationType::NotSeenCall, NotSeenCallNotification>(std::move(record));
            break;
        case NotificationsRecord::Key::Sms:
            notificationsChanged |=
                handleNotSeenWithCounter<NotificationType::NotSeenSms, NotSeenSMSNotification>(std::move(record));
            break;
        default:
            break;
        }
    }

    if (notificationsChanged) {
        send();
    }
}

void NotificationProvider::handle(db::NotificationMessage *msg)
{
    if (msg->interface == db::Interface::Name::Notifications && msg->type == db::Query::Type::Update) {
        requestNotSeenNotifications();
    }
}

void NotificationProvider::handle(sys::phone_modes::Tethering tethering)
{
    using Tethering           = sys::phone_modes::Tethering;
    bool notificationsChanged = false;

    if (tethering == Tethering::On && notifications.count(NotificationType::Tethering) == 0) {
        notifications[NotificationType::Tethering] = std::make_shared<notifications::TetheringNotification>();
        notificationsChanged                       = true;
    }
    else if (tethering == Tethering::Off && notifications.count(NotificationType::Tethering) != 0) {
        notifications.erase(NotificationType::Tethering);
        notificationsChanged = true;
    }

    if (notificationsChanged) {
        send();
    }
}

void NotificationProvider::requestNotSeenNotifications()
{
    DBServiceAPI::GetQuery(
        ownerService, db::Interface::Name::Notifications, std::make_unique<db::query::notifications::GetAll>());
}

namespace
{
    using mapType    = NotificationProvider::Notifications;
    using returnType = std::shared_ptr<Notification>;
    struct get_second : public std::unary_function<mapType::value_type, returnType>
    {
        returnType operator()(const mapType::value_type &value) const
        {
            return value.second;
        }
    };
} // namespace

void NotificationProvider::send()
{
    std::list<std::shared_ptr<const Notification>> toSendNotifications;
    transform(notifications.begin(), notifications.end(), back_inserter(toSendNotifications), get_second());

    toSendNotifications.sort(
        [](const std::shared_ptr<const Notification> &lhs, const std::shared_ptr<const Notification> &rhs) {
            return (lhs->getPriority() > rhs->getPriority());
        });

    app::manager::Controller::sendAction(
        ownerService,
        app::manager::actions::NotificationsChanged,
        std::make_unique<app::manager::actions::NotificationsChangedParams>(std::move(toSendNotifications)));
}