UniversalSwitch application context 69/139169/5
authorMariusz Wachowicz <m.wachowicz@partner.samsung.com>
Mon, 17 Jul 2017 15:39:25 +0000 (17:39 +0200)
committerMariusz Wachowicz <m.wachowicz@partner.samsung.com>
Tue, 18 Jul 2017 15:48:28 +0000 (17:48 +0200)
adds UniversalSwitch singleton class for handles management
AcceptScanningActivity has to be removed after integration with SelectActivity

Change-Id: I2669a6b184fde85bd7df1661eb4bf8b5335015cf

13 files changed:
src/AcceptScanningActivity.cpp [new file with mode: 0644]
src/AccessoriesSwitchProvider.cpp
src/CMakeLists.txt
src/CompositeSwitchProvider.cpp
src/CompositeSwitchProvider.hpp
src/Singleton.hpp
src/Subject.hpp
src/UIActivity.hpp
src/UniversalSwitch.cpp [new file with mode: 0644]
src/UniversalSwitch.hpp [new file with mode: 0644]
src/UniversalSwitchLog.hpp
src/main.cpp [new file with mode: 0644]
src/universalswitch.cpp [deleted file]

diff --git a/src/AcceptScanningActivity.cpp b/src/AcceptScanningActivity.cpp
new file mode 100644 (file)
index 0000000..705b7c8
--- /dev/null
@@ -0,0 +1,20 @@
+#include "UIActivity.hpp"
+#include "ActivityFactory.hpp"
+#include "UniversalSwitchLog.hpp"
+#include "ScreenScannerManager.hpp"
+
+//TODO: ramove after integration with SelectActivity
+class AcceptScanningActivity : public UIActivity, private RegisterActivity<AcceptScanningActivity>
+{
+public:
+       constexpr static const char *activityType = "ACCEPT_SCANNING";
+       AcceptScanningActivity()
+               : UIActivity(activityType)
+       {}
+
+       bool process() override
+       {
+               ScreenScannerManager::instance().acceptAutoscanning();
+               return true;
+       }
+};
index 045fbee..6e43ea3 100644 (file)
@@ -707,10 +707,10 @@ Eina_Bool AccessoriesSwitchProvider::onEcoreEventKeyDownCb(void *data, int type,
        auto event = static_cast<Ecore_Event_Key *>(ev);
        std::string keyId(event->key);
 
+       //TODO: static_cast should work fine here however it is causing runtime errors in animation rendering
        auto accessoriesSwitchProvider = reinterpret_cast<AccessoriesSwitchProvider *>(data);
        auto switchId = SwitchId(keyId, accessoriesSwitchProvider->getId());
        auto sw = accessoriesSwitchProvider->findSwitchById(switchId.getGlobalId());
-
        if (sw)
                accessoriesSwitchProvider->notify(sw);
 
index 6dd5df8..4c36fc3 100644 (file)
@@ -1,9 +1,9 @@
 SET(SRCS
+       main.cpp
        AccessoriesSwitchProvider.cpp
        QueryBuilder.cpp
        Switch.cpp
        SwitchProvider.cpp
-       universalswitch.cpp
        Activity.cpp
        CompositeSwitchProvider.cpp
        SwitchConfigurationItem.cpp
@@ -23,6 +23,8 @@ SET(SRCS
        AutoScanOnActivity.cpp
        MenuBuilder.cpp
        Atspi.cpp
+       UniversalSwitch.cpp
+       AcceptScanningActivity.cpp
 )
 
 ADD_LIBRARY(universal-switch-obj OBJECT ${SRCS})
index 91c5e86..faee7c8 100644 (file)
@@ -37,3 +37,19 @@ std::vector<std::shared_ptr<SwitchProvider>> CompositeSwitchProvider::getProvide
 {
        return providers;
 }
+
+void CompositeSwitchProvider::attach(std::weak_ptr<Observer<Switch>> observer, std::shared_ptr<Condition<Switch>> cond)
+{
+       auto item = cond->getItem();
+       if (item) {
+               auto providerId = item->getId()->getProviderId();
+               for (auto &provider : providers)
+                       if (provider->getId() == providerId) {
+                               provider->attach(observer, std::move(cond));
+                               return;
+                       }
+       } else {
+               for (auto &provider : providers)
+                       provider->attach(observer, cond);
+       }
+}
index 9f96903..cc9a387 100644 (file)
@@ -14,6 +14,7 @@ public:
        std::shared_ptr<Switch> findSwitchById(const std::string &id) const override;
        void add(std::shared_ptr<SwitchProvider> provider);
        std::vector<std::shared_ptr<SwitchProvider>> getProviders();
+       void attach(std::weak_ptr<Observer<Switch>> observer, std::shared_ptr<Condition<Switch>> cond) override;
 
 private:
        std::vector<std::shared_ptr<SwitchProvider>> providers;
index 052ce8d..6bc31ac 100644 (file)
@@ -15,6 +15,17 @@ protected:
        SingletonBase &operator = (SingletonBase &&) = delete;
 };
 
+template <class Implementation>
+class Singleton : protected SingletonBase
+{
+public:
+       static Implementation &instance()
+       {
+               static Implementation implementation;
+               return implementation;
+       }
+};
+
 template <class Interface>
 class MockableSingleton : protected SingletonBase
 {
index e5be8b2..5c068c5 100644 (file)
@@ -10,7 +10,7 @@ template <typename T>
 class Subject
 {
 public:
-       void attach(std::weak_ptr<Observer<T>> observer, std::unique_ptr<Condition<T>> cond)
+       virtual void attach(std::weak_ptr<Observer<T>> observer, std::shared_ptr<Condition<T>> cond)
        {
                observers.emplace_back(std::move(observer), std::move(cond));
        }
@@ -39,7 +39,7 @@ protected:
                }
        }
 
-       std::vector<std::pair<std::weak_ptr<Observer<T>>, std::unique_ptr<Condition<T>>>> observers;
+       std::vector<std::pair<std::weak_ptr<Observer<T>>, std::shared_ptr<Condition<T>>>> observers;
 };
 
 #endif
index 9a7dc04..a0c91a5 100644 (file)
@@ -17,7 +17,7 @@ public:
 
        void update(const std::shared_ptr<UIElement> &elem) override;
 
-private:
+protected:
        std::shared_ptr<UIElement> uiElement;
 };
 
diff --git a/src/UniversalSwitch.cpp b/src/UniversalSwitch.cpp
new file mode 100644 (file)
index 0000000..a79e039
--- /dev/null
@@ -0,0 +1,21 @@
+#include "UniversalSwitch.hpp"
+
+std::shared_ptr<SwitchManager> UniversalSwitch::getSwitchManager() const
+{
+       return switchManager;
+}
+
+std::shared_ptr<CompositeSwitchProvider> UniversalSwitch::getCompositeSwitchProvider() const
+{
+       return compositeSwitchProvider;
+}
+
+void UniversalSwitch::setSwitchManager(const std::shared_ptr<SwitchManager> &sm)
+{
+       switchManager = sm;
+}
+
+void UniversalSwitch::setCompositeSwitchProvider(const std::shared_ptr<CompositeSwitchProvider> &csp)
+{
+       compositeSwitchProvider = csp;
+}
\ No newline at end of file
diff --git a/src/UniversalSwitch.hpp b/src/UniversalSwitch.hpp
new file mode 100644 (file)
index 0000000..48340c9
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef UNIVERSAL_SWITCH_HPP
+#define UNIVERSAL_SWITCH_HPP
+
+#include "Singleton.hpp"
+
+#include "SwitchManager.hpp"
+#include "CompositeSwitchProvider.hpp"
+
+struct UniversalSwitch : public Singleton<UniversalSwitch> {
+public:
+       std::shared_ptr<SwitchManager> getSwitchManager() const;
+       std::shared_ptr<CompositeSwitchProvider> getCompositeSwitchProvider() const;
+
+       void setSwitchManager(const std::shared_ptr<SwitchManager> &sm);
+       void setCompositeSwitchProvider(const std::shared_ptr<CompositeSwitchProvider> &csp);
+
+private:
+       std::shared_ptr<SwitchManager> switchManager;
+       std::shared_ptr<CompositeSwitchProvider> compositeSwitchProvider;
+};
+
+#endif
index d64a3a5..06f0a98 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef UNIVERSAL_SWITCH_HPP
-#define UNIVERSAL_SWITCH_HPP
+#ifndef UNIVERSAL_SWITCH_LOG_HPP
+#define UNIVERSAL_SWITCH_LOG_HPP
 
 #include <libintl.h>
 #include <dlog.h>
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644 (file)
index 0000000..12ea8a6
--- /dev/null
@@ -0,0 +1,92 @@
+#include "UniversalSwitchLog.hpp"
+#include "Window.hpp"
+#include "ScreenScannerManager.hpp"
+#include "Atspi.hpp"
+
+#include "UniversalSwitch.hpp"
+#include "CompositeSwitchProvider.hpp"
+#include "AccessoriesSwitchProvider.hpp"
+#include "SQLiteConfiguration.hpp"
+#include "SwitchConfigurationItem.hpp"
+#include "ActivityFactory.hpp"
+#include "SwitchManager.hpp"
+
+#include <tizen.h>
+#include <app.h>
+#include <Elementary.h>
+
+static void _setting_time_lang_changed(app_event_info_h event_info, void *data)
+{
+       char *lang = NULL;
+       if (app_event_get_language(event_info, &lang) == APP_ERROR_NONE) {
+               DEBUG("Language is changed : %s", lang);
+               elm_language_set(lang);
+               elm_config_all_flush();
+               free(lang);
+       } else {
+               ERROR("Cannot get language from event_info");
+       }
+}
+
+bool app_create(void *data)
+{
+       DEBUG("App create");
+       ecore_event_init();
+       Atspi::ConnectAtClient();
+
+       auto activityFactory = ActivityFactory::getInstance();
+
+       auto compositeSwitchProvider = std::make_shared<CompositeSwitchProvider>();
+       compositeSwitchProvider->add(std::make_shared<AccessoriesSwitchProvider>());
+       //TODO: add ScreenSwitchProvider when available
+       //TDOO: add CameraSwitchProvider when available
+
+       //TODO: temporarily memory database is used (change to file database)
+       auto configuration = std::make_shared<SQLiteConfiguration>(true);
+
+       {
+               //TODO: remove after integration with settings app
+               std::string switchId = "AccessoriesSwitchProvider_x";
+               auto switch_ = compositeSwitchProvider->findSwitchById(switchId);
+               std::string activityType = "ACCEPT_SCANNING";
+               auto item = std::make_shared<SwitchConfigurationItem>(switch_->getId()->getGlobalId(), activityType);
+               DEBUG("getGlobalId %s", switch_->getId()->getGlobalId().c_str());
+               configuration->add(item);
+       }
+
+       UniversalSwitch::instance().setSwitchManager(SwitchManager::create<SwitchManager>(compositeSwitchProvider, configuration, activityFactory));
+       UniversalSwitch::instance().setCompositeSwitchProvider(compositeSwitchProvider);
+
+       //TODO: move to navigation context changed signal
+       ScreenScannerManager::instance().startAutoscanning();
+
+       return true;
+}
+
+void app_terminate(void *data)
+{
+       DEBUG("app termination procedure");
+       ecore_event_shutdown();
+       Atspi::DisconnectAtClient();
+       ScreenScannerManager::instance().stopAutoscanning();
+
+       return;
+}
+
+int main(int argc, char *argv[])
+{
+       DEBUG("main function called");
+
+       ui_app_lifecycle_callback_s event_callback = {
+               .create = app_create,
+               .terminate = app_terminate,
+               .pause = nullptr,
+               .resume = nullptr,
+               .app_control = nullptr
+       };
+
+       app_event_handler_h handler;
+       ui_app_add_event_handler(&handler, APP_EVENT_LANGUAGE_CHANGED, _setting_time_lang_changed, nullptr);
+
+       return ui_app_main(argc, argv, &event_callback, nullptr);
+}
diff --git a/src/universalswitch.cpp b/src/universalswitch.cpp
deleted file mode 100644 (file)
index e239707..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-#include "UniversalSwitchLog.hpp"
-#include "Window.hpp"
-#include "ScreenScannerManager.hpp"
-#include "Atspi.hpp"
-
-#include <tizen.h>
-#include <app.h>
-#include <Elementary.h>
-
-static void _setting_time_lang_changed(app_event_info_h event_info, void *data)
-{
-       char *lang = NULL;
-       if (app_event_get_language(event_info, &lang) == APP_ERROR_NONE) {
-               DEBUG("Language is changed : %s", lang);
-               elm_language_set(lang);
-               elm_config_all_flush();
-               free(lang);
-       } else {
-               ERROR("Cannot get language from event_info");
-       }
-}
-
-bool app_create(void *data)
-{
-       DEBUG("App create");
-       Atspi::ConnectAtClient();
-
-       return true;
-}
-
-void app_terminate(void *data)
-{
-       DEBUG("app termination procedure");
-       Atspi::DisconnectAtClient();
-
-       return;
-}
-
-void app_control(app_control_h app_control, void *data)
-{
-       return;
-}
-
-int main(int argc, char *argv[])
-{
-       DEBUG("main function called");
-
-       ui_app_lifecycle_callback_s event_callback = {
-               .create = app_create,
-               .terminate = app_terminate,
-               .pause = nullptr,
-               .resume = nullptr,
-               .app_control = app_control
-       };
-
-       app_event_handler_h handler;
-       ui_app_add_event_handler(&handler, APP_EVENT_LANGUAGE_CHANGED, _setting_time_lang_changed, nullptr);
-
-       return ui_app_main(argc, argv, &event_callback, nullptr);
-}