~aleteoryx/muditaos

muditaos/module-apps/apps-common/ApplicationLauncher.hpp -rw-r--r-- 3.6 KiB
a405cad6Aleteoryx trim readme 6 days 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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#pragma once

namespace app
{
    using ApplicationManifest = app::manager::ApplicationManifest;

    enum class Closeable : bool
    {
        False,
        True
    };
    /// used in ApplicationManager to start applications
    class ApplicationLauncher
    {
      protected:
        /// name of the application to run
        std::string name;
        /// name of the application's owner
        std::string parent;
        /// Application's manifest
        ApplicationManifest manifest;
        /// defines whether application can be closed when it looses focus
        Closeable closeable = Closeable::False;
        /// defines whether application should be run without gaining focus, it will remian in the BACKGROUND state
        bool startBackground = false;

        void setAutoLockPolicy() noexcept
        {
            if (handle != nullptr) {
                handle->getLockPolicyHandler().set(manifest.getAutoLockPolicy());
            }
        }

      public:
        ApplicationLauncher(std::string name, ApplicationManifest &&manifest, Closeable isCloseable)
            : name{std::move(name)}, manifest{std::move(manifest)}, closeable{isCloseable} {};
        virtual ~ApplicationLauncher() = default;

        [[nodiscard]] std::string getName() const noexcept
        {
            return name;
        }

        [[nodiscard]] const ApplicationManifest &getManifest() const noexcept
        {
            return manifest;
        }

        [[nodiscard]] bool isCloseable() const noexcept
        {
            return (closeable == Closeable::True);
        }

        [[nodiscard]] bool preventsAutoLocking() const noexcept
        {
            return (handle == nullptr || handle->getLockPolicyHandler().preventsAutoLocking());
        }

        virtual bool run(StatusIndicators statusIndicators, sys::Service *caller = nullptr)
        {
            return false;
        }

        virtual bool runBackground(StatusIndicators statusIndicators, sys::Service *caller = nullptr)
        {
            return false;
        }

        std::shared_ptr<ApplicationCommon> handle = nullptr;
    };

    /// application launcher boilerplate
    template <class T>
    class ApplicationLauncherT : public ApplicationLauncher
    {
      public:
        ApplicationLauncherT(std::string name, ApplicationManifest &&manifest, Closeable isCloseable = Closeable::True)
            : ApplicationLauncher(std::move(name), std::move(manifest), isCloseable)
        {}

        bool run(StatusIndicators statusIndicators, sys::Service *caller) override
        {
            parent = (caller == nullptr ? "" : caller->GetName());
            handle = std::make_shared<T>(name, parent, statusIndicators);
            setAutoLockPolicy();
            return sys::SystemManagerCommon::RunApplication(handle, caller);
        }

        bool runBackground(StatusIndicators statusIndicators, sys::Service *caller) override
        {
            parent = (caller == nullptr ? "" : caller->GetName());
            handle = std::make_shared<T>(name, parent, statusIndicators, true);
            setAutoLockPolicy();
            return sys::SystemManagerCommon::RunApplication(handle, caller);
        }
    };

    /// creates application launcher per class provided
    template <class T>
    std::unique_ptr<ApplicationLauncherT<T>> CreateLauncher(std::string name, Closeable isCloseable = Closeable::True)
    {
        return std::unique_ptr<ApplicationLauncherT<T>>(
            new ApplicationLauncherT<T>(name, ManifestOf<T>(), isCloseable));
    }
} // namespace app