// 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 #include #include #include #include #include using namespace notifications; NotificationProvider::NotificationProvider(sys::Service *ownerService) : ownerService{ownerService} {} template 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(notifications[type].get()); value > notification->getValue()) { notifications[type] = std::make_shared(value, std::move(record.contactRecord)); return true; } } else if (value > 0) { notifications[type] = std::make_shared(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(std::move(record)); break; case NotificationsRecord::Key::Sms: notificationsChanged |= handleNotSeenWithCounter(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(); 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()); } namespace { using mapType = NotificationProvider::Notifications; using returnType = std::shared_ptr; struct get_second : public std::unary_function { returnType operator()(const mapType::value_type &value) const { return value.second; } }; } // namespace void NotificationProvider::send() { std::list> toSendNotifications; transform(notifications.begin(), notifications.end(), back_inserter(toSendNotifications), get_second()); toSendNotifications.sort( [](const std::shared_ptr &lhs, const std::shared_ptr &rhs) { return (lhs->getPriority() > rhs->getPriority()); }); app::manager::Controller::sendAction( ownerService, app::manager::actions::NotificationsChanged, std::make_unique(std::move(toSendNotifications))); }