~aleteoryx/muditaos

ref: d9a1194e6f203247ebcef4b03f8ce5ebccc7c778 muditaos/module-apps/application-music-player/ApplicationMusicPlayer.cpp -rw-r--r-- 7.0 KiB
d9a1194e — Lukasz Mastalerz [BH-1688] Create a standard for logs 2 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <application-music-player/ApplicationMusicPlayer.hpp>

#include "AudioNotificationsHandler.hpp"

#include <windows/MusicPlayerMainWindow.hpp>
#include <windows/MusicPlayerAllSongsWindow.hpp>
#include <apps-common/AudioOperations.hpp>
#include <presenters/SongsPresenter.hpp>
#include <models/SongsRepository.hpp>
#include <models/SongsModel.hpp>
#include <service-appmgr/Controller.hpp>

#include <filesystem>
#include <log/log.hpp>
#include <i18n/i18n.hpp>
#include <purefs/filesystem_paths.hpp>
#include <service-audio/AudioServiceAPI.hpp>
#include <time/ScopedTime.hpp>

namespace app
{
    namespace music_player::internal
    {
        class MusicPlayerPriv
        {
          public:
            std::shared_ptr<app::music::SongsModelInterface> songsModel;
            std::shared_ptr<app::music_player::SongsContract::Presenter> songsPresenter;
        };
    } // namespace music_player::internal

    constexpr std::size_t applicationMusicPlayerStackSize = 5 * 1024;

    ApplicationMusicPlayer::ApplicationMusicPlayer(std::string name,
                                                   std::string parent,
                                                   StatusIndicators statusIndicators,
                                                   StartInBackground startInBackground)
        : Application(
              std::move(name), std::move(parent), statusIndicators, startInBackground, applicationMusicPlayerStackSize),
          priv{std::make_unique<music_player::internal::MusicPlayerPriv>()}
    {
        bus.channels.push_back(sys::BusChannel::ServiceAudioNotifications);

        const auto paths     = std::vector<std::string>{purefs::dir::getUserMediaPath()};
        auto tagsFetcher     = std::make_unique<app::music::ServiceAudioTagsFetcher>(this);
        auto songsRepository = std::make_unique<app::music::SongsRepository>(this, std::move(tagsFetcher), paths);

        priv->songsModel     = std::make_unique<app::music::SongsModel>(this, std::move(songsRepository));
        auto audioOperations = std::make_unique<app::AsyncAudioOperations>(this);
        priv->songsPresenter =
            std::make_unique<app::music_player::SongsPresenter>(this, priv->songsModel, std::move(audioOperations));

        // callback used when playing state is changed
        using SongState                                 = app::music::SongState;
        std::function<void(SongState)> autolockCallback = [this](SongState isPlaying) {
            if (isPlaying == SongState::Playing) {
                LOG_DEBUG("Preventing autolock while playing track.");
                lockPolicyHandler.set(locks::AutoLockPolicy::DetermineByAppState);
            }
            else {
                LOG_DEBUG("Autolock reenabled because track is no longer playing.");
                lockPolicyHandler.set(locks::AutoLockPolicy::DetermineByWindow);
                app::manager::Controller::preventBlockingDevice(this);
            }
        };
        priv->songsPresenter->setPlayingStateCallback(std::move(autolockCallback));

        // callback used when track is not played and we are in DetermineByAppState
        std::function<bool()> stateLockCallback = []() -> bool { return true; };
        lockPolicyHandler.setPreventsAutoLockByStateCallback(std::move(stateLockCallback));

        connect(typeid(AudioStopNotification), [&](sys::Message *msg) -> sys::MessagePointer {
            const auto notification = static_cast<AudioStopNotification *>(msg);
            music_player::AudioNotificationsHandler audioNotificationHandler{priv->songsPresenter};
            return audioNotificationHandler.handleAudioStopNotification(notification);
        });
        connect(typeid(AudioEOFNotification), [&](sys::Message *msg) -> sys::MessagePointer {
            const auto notification = static_cast<AudioStopNotification *>(msg);
            music_player::AudioNotificationsHandler audioNotificationHandler{priv->songsPresenter};
            return audioNotificationHandler.handleAudioEofNotification(notification);
        });
        connect(typeid(AudioPausedNotification), [&](sys::Message *msg) -> sys::MessagePointer {
            const auto notification = static_cast<AudioPausedNotification *>(msg);
            music_player::AudioNotificationsHandler audioNotificationHandler{priv->songsPresenter};
            return audioNotificationHandler.handleAudioPausedNotification(notification);
        });
        connect(typeid(AudioResumedNotification), [&](sys::Message *msg) -> sys::MessagePointer {
            const auto notification = static_cast<AudioResumedNotification *>(msg);
            music_player::AudioNotificationsHandler audioNotificationHandler{priv->songsPresenter};
            return audioNotificationHandler.handleAudioResumedNotification(notification);
        });
    }

    ApplicationMusicPlayer::~ApplicationMusicPlayer() = default;

    sys::MessagePointer ApplicationMusicPlayer::DataReceivedHandler(sys::DataMessage *msgl,
                                                                    [[maybe_unused]] sys::ResponseMessage *resp)
    {
        const auto retMsg = Application::DataReceivedHandler(msgl);
        // if message was handled by application's template there is no need to process further.
        if (static_cast<sys::ResponseMessage *>(retMsg.get())->retCode == sys::ReturnCodes::Success) {
            return retMsg;
        }

        return handleAsyncResponse(resp);
    }

    // Invoked during initialization
    sys::ReturnCodes ApplicationMusicPlayer::InitHandler()
    {
        const auto ret = Application::InitHandler();
        if (ret != sys::ReturnCodes::Success) {
            return ret;
        }

        createUserInterface();
        return ret;
    }

    sys::ReturnCodes ApplicationMusicPlayer::DeinitHandler()
    {
        priv->songsPresenter->getMusicPlayerModelInterface()->clearData();
        priv->songsPresenter->stop();
        return Application::DeinitHandler();
    }

    void ApplicationMusicPlayer::createUserInterface()
    {
        windowsFactory.attach(gui::name::window::main_window, [&](ApplicationCommon *app, const std::string &name) {
            return std::make_unique<gui::MusicPlayerMainWindow>(app, priv->songsPresenter);
        });

        windowsFactory.attach(gui::name::window::all_songs_window,
                              [&](ApplicationCommon *app, const std::string &name) {
                                  return std::make_unique<gui::MusicPlayerAllSongsWindow>(app, priv->songsPresenter);
                              });
        attachPopups({gui::popup::ID::Volume,
                      gui::popup::ID::Tethering,
                      gui::popup::ID::BluetoothAuthenticate,
                      gui::popup::ID::PhoneModes,
                      gui::popup::ID::PhoneLock,
                      gui::popup::ID::SimLock,
                      gui::popup::ID::Alarm});
    }

    void ApplicationMusicPlayer::destroyUserInterface()
    {}
} /* namespace app */