// Copyright (c) 2017-2020, 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; /// 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 bool closeable = true; /// 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 power manager from changing bool preventBlocking = false; public: ApplicationLauncher(std::string name, ApplicationManifest &&manifest, bool isCloseable, bool preventBlocking = false) : name{name}, manifest{std::move(manifest)}, closeable{isCloseable}, preventBlocking{preventBlocking} {}; 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; } [[nodiscard]] bool isBlocking() const noexcept { return preventBlocking; } virtual bool run(sys::Service *caller = nullptr) { return false; } virtual bool runBackground(sys::Service *caller = nullptr) { return false; } std::shared_ptr handle = nullptr; }; /// application launcher boilerplate template class ApplicationLauncherT : public ApplicationLauncher { public: ApplicationLauncherT(std::string name, ApplicationManifest &&manifest, bool isCloseable = true) : ApplicationLauncher(name, std::move(manifest), isCloseable) {} bool run(sys::Service *caller) override { parent = (caller == nullptr ? "" : caller->GetName()); handle = std::make_shared(name, parent); return sys::SystemManager::CreateService(handle, caller); } bool runBackground(sys::Service *caller) override { parent = (caller == nullptr ? "" : caller->GetName()); handle = std::make_shared(name, parent, true); return sys::SystemManager::CreateService(handle, caller); } }; /// creates application launcher per class provided template std::unique_ptr> CreateLauncher(std::string name, bool isCloseable = true) { return std::unique_ptr>( new ApplicationLauncherT(name, ManifestOf(), isCloseable)); } } // namespace app