~aleteoryx/muditaos

ref: 3c69244f4db9f310c328687052326659aad29146 muditaos/module-apps/ApplicationLauncher.hpp -rw-r--r-- 4.1 KiB
3c69244f — Przemyslaw Brudny [EGD-6773] Added light BarGraph type and updated UpdateProgress 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "Application.hpp"

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

    enum class PreventAutoLocking : bool
    {
        False,
        True
    };
    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;
        /// flag defines whether this application can prevent auto-locking mechanism
        PreventAutoLocking preventAutoLocking = PreventAutoLocking::False;

      public:
        ApplicationLauncher(std::string name,
                            ApplicationManifest &&manifest,
                            Closeable isCloseable,
                            PreventAutoLocking preventAutoLocking = PreventAutoLocking::False)
            : name{std::move(name)}, manifest{std::move(manifest)}, closeable{isCloseable}, preventAutoLocking{
                                                                                                preventAutoLocking} {};
        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 isPreventAutoLockingOn() const noexcept
        {
            return (preventAutoLocking == PreventAutoLocking::True);
        }

        virtual bool run(sys::phone_modes::PhoneMode mode, sys::Service *caller = nullptr)
        {
            return false;
        }

        virtual bool runBackground(sys::phone_modes::PhoneMode mode, sys::Service *caller = nullptr)
        {
            return false;
        }

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

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

        bool run(sys::phone_modes::PhoneMode mode, sys::Service *caller) override
        {
            parent = (caller == nullptr ? "" : caller->GetName());
            handle = std::make_shared<T>(name, parent, mode);
            return sys::SystemManager::RunApplication(handle, caller);
        }

        bool runBackground(sys::phone_modes::PhoneMode mode, sys::Service *caller) override
        {
            parent = (caller == nullptr ? "" : caller->GetName());
            handle = std::make_shared<T>(name, parent, mode, true);
            return sys::SystemManager::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,
        PreventAutoLocking preventAutoLocking = PreventAutoLocking::False)
    {
        return std::unique_ptr<ApplicationLauncherT<T>>(
            new ApplicationLauncherT<T>(name, ManifestOf<T>(), isCloseable, preventAutoLocking));
    }
} // namespace app