From 099dbc1d3732b4eab903a06e37e152bee017f6e5 Mon Sep 17 00:00:00 2001 From: libenyao Date: Thu, 23 Feb 2023 16:26:35 +0800 Subject: [PATCH] add jsapp/BUILD.gn CMakeList.txt JsApp.cpp JsApp.h and jsapp/lite Signed-off-by: libenyao --- jsapp/BUILD.gn | 71 ++++++++ jsapp/CMakeLists.txt | 5 + jsapp/JsApp.cpp | 180 ++++++++++++++++++++ jsapp/JsApp.h | 80 +++++++++ jsapp/lite/JsAppImpl.cpp | 289 ++++++++++++++++++++++++++++++++ jsapp/lite/JsAppImpl.h | 55 ++++++ jsapp/lite/TimerTaskHandler.cpp | 111 ++++++++++++ jsapp/lite/TimerTaskHandler.h | 34 ++++ 8 files changed, 825 insertions(+) create mode 100644 jsapp/BUILD.gn create mode 100644 jsapp/CMakeLists.txt create mode 100644 jsapp/JsApp.cpp create mode 100644 jsapp/JsApp.h create mode 100644 jsapp/lite/JsAppImpl.cpp create mode 100644 jsapp/lite/JsAppImpl.h create mode 100644 jsapp/lite/TimerTaskHandler.cpp create mode 100644 jsapp/lite/TimerTaskHandler.h diff --git a/jsapp/BUILD.gn b/jsapp/BUILD.gn new file mode 100644 index 0000000..8a4928f --- /dev/null +++ b/jsapp/BUILD.gn @@ -0,0 +1,71 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") +import("../gn/config.gni") +config("jsapp_config") { + cflags = [ + "-std=c++17", + "-Wall", + "-Wno-deprecated-declarations", + ] + cflags_cc = cflags +} + +ohos_static_library("jsapp_rich") { + configs = [ ":jsapp_config" ] + sources = [ + "JsApp.cpp", + "rich/JsAppImpl.cpp", + ] + include_dirs = [ + ".", + "rich/", + "../mock/rich/", + "../util/", + "../mock/", + "//third_party/jsoncpp/include/json/", + ] + include_dirs += os_include_dirs + deps = [ "//third_party/libwebsockets:websockets_static" ] +} + +ohos_static_library("jsapp_lite") { + configs = [ ":jsapp_config" ] + sources = [ + "JsApp.cpp", + "lite/JsAppImpl.cpp", + "lite/TimerTaskHandler.cpp", + "lite/ui_text_language.cpp", + ] + include_dirs = [ + ".", + "lite/", + "../mock/lite/", + "../util/", + "../mock/", + "../cli", + "//third_party/jsoncpp/include/json/", + "//foundation/graphic/ui/interfaces/innerkits/", + "//foundation/graphic/ui/interfaces/kits/", + "//foundation/graphic/utils/interfaces/innerkits/", + "//foundation/graphic/utils/interfaces/kits/", + "//foundation/graphic/ui/frameworks/", + "//foundation/graphic/ui/frameworks/dock/", + "//foundation/graphic/ui/interfaces/innerkits/dock/", + ] + deps = [ + "//foundation/arkui/ace_engine_lite/frameworks/targets/simulator:ace_lite", + "//third_party/libwebsockets:websockets_static", + ] +} diff --git a/jsapp/CMakeLists.txt b/jsapp/CMakeLists.txt new file mode 100644 index 0000000..6bb4c4e --- /dev/null +++ b/jsapp/CMakeLists.txt @@ -0,0 +1,5 @@ +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} jsapp) +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/lite jsapp_lite) +add_library(jsapp STATIC ${jsapp} ${jsapp_lite}) +target_include_directories(jsapp PUBLIC ${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}/lite) +target_link_libraries(jsapp cli util mock ui jsfwk) diff --git a/jsapp/JsApp.cpp b/jsapp/JsApp.cpp new file mode 100644 index 0000000..2ba36df --- /dev/null +++ b/jsapp/JsApp.cpp @@ -0,0 +1,180 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "JsApp.h" + +#include "FileSystem.h" +#include "JsAppImpl.h" +#include "PreviewerEngineLog.h" + +using namespace std; + +JsApp::JsApp() + : pipeName(""), + pipePort(""), + jsAppPath(""), + bundleName("undefined"), + urlPath(""), + isFinished(true), + isRunning(true), + isDebug(false), + debugServerPort(0), + jsHeapSize(0), + colorMode(""), + orientation(""), + aceVersion(""), + screenDensity(""), + configChanges("") +{ +} + +void JsApp::Stop() +{ + ILOG("JsApp::Stop start stop js app."); + auto start = std::chrono::system_clock::now(); + while (!isFinished) { + JsAppImpl::GetInstance().Interrupt(); + this_thread::sleep_for(chrono::milliseconds(1)); + auto end = std::chrono::system_clock::now(); + auto passedSecond = chrono::duration_cast(end - start).count(); + if (passedSecond > 10) { // The restart timeout interval is 10 seconds. + ILOG("Restart js app time out!"); + return; + } + } + ILOG("JsApp::Stop js app stop finished."); +} + +bool JsApp::IsLiteDevice(std::string deviceType) +{ + auto iter = find(liteDevice.begin(), liteDevice.end(), deviceType); + if (iter == liteDevice.end()) { + return false; + } + return true; +} + +void JsApp::SetPipeName(const std::string& value) +{ + pipeName = value; +} + +void JsApp::SetPipePort(const std::string& value) +{ + pipePort = value; +} + +void JsApp::SetJsAppPath(const string& value) +{ + jsAppPath = value; +} + +void JsApp::SetScreenDensity(const std::string value) +{ + screenDensity = value; +} + +void JsApp::SetConfigChanges(const std::string value) +{ + configChanges = value; +} + +void JsApp::SetUrlPath(const string& value) +{ + urlPath = value; +} + +void JsApp::SetBundleName(const string& name) +{ + bundleName = name; + FileSystem::SetBundleName(name); + FileSystem::MakeVirtualFileSystemPath(); +} + +void JsApp::SetRunning(bool flag) +{ + isRunning = flag; +} + +bool JsApp::GetRunning() +{ + return isRunning; +} + +void JsApp::SetIsDebug(bool value) +{ + isDebug = value; +} + +void JsApp::SetDebugServerPort(uint16_t value) +{ + debugServerPort = value; +} + +void JsApp::SetJSHeapSize(uint32_t size) +{ + jsHeapSize = size; +} + +string JsApp::GetJSONTree() +{ + return ""; +} + +string JsApp::GetDefaultJSONTree() +{ + return ""; +} + +void JsApp::SetArgsColorMode(const string& value) +{ + colorMode = value; +} + +void JsApp::SetArgsAceVersion(const string& value) +{ + aceVersion = value; +} + +void JsApp::ColorModeChanged(const std::string commandColorMode) +{ + colorMode = commandColorMode; +}; + +std::string JsApp::GetColorMode() const +{ + return colorMode; +} + +std::string JsApp::GetOrientation() const +{ + return orientation; +} + +void JsApp::OrientationChanged(std::string commandOrientation) +{ + orientation = commandOrientation; +}; + +void JsApp::ResolutionChanged(int32_t, int32_t, int32_t, int32_t, int32_t){}; + +void JsApp::ReloadRuntimePage(const std::string){}; + +bool JsApp::MemoryRefresh(const std::string) const +{ + return false; +} + +void JsApp::LoadDocument(const std::string, const std::string, const Json::Value) {}; diff --git a/jsapp/JsApp.h b/jsapp/JsApp.h new file mode 100644 index 0000000..0159ef9 --- /dev/null +++ b/jsapp/JsApp.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef JSAPP_H +#define JSAPP_H + +#include +#include +#include +#include +#include "json.h" + +class JsApp { +public: + JsApp& operator=(const JsApp&) = delete; + JsApp(const JsApp&) = delete; + virtual void Start() = 0; + virtual void Restart() = 0; + virtual void Interrupt() = 0; + void Stop(); + void SetJsAppPath(const std::string& value); + void SetUrlPath(const std::string& value); + void SetPipeName(const std::string& name); + void SetPipePort(const std::string& port); + void SetBundleName(const std::string& name); + void SetRunning(bool flag); + bool GetRunning(); + void SetIsDebug(bool value); + void SetDebugServerPort(uint16_t value); + void SetJSHeapSize(uint32_t size); + virtual std::string GetJSONTree(); + virtual std::string GetDefaultJSONTree(); + virtual void OrientationChanged(std::string); + virtual void ResolutionChanged(int32_t, int32_t, int32_t, int32_t, int32_t); + virtual void SetArgsColorMode(const std::string& name); + virtual void SetArgsAceVersion(const std::string& aceVersion); + virtual std::string GetOrientation() const; + virtual std::string GetColorMode() const; + virtual void ColorModeChanged(const std::string); + static bool IsLiteDevice(std::string); + virtual void ReloadRuntimePage(const std::string); + virtual void SetScreenDensity(const std::string); + virtual void SetConfigChanges(const std::string); + virtual bool MemoryRefresh(const std::string) const; + virtual void LoadDocument(const std::string, const std::string, const Json::Value); + +protected: + JsApp(); + virtual ~JsApp(){}; + std::string pipeName; + std::string pipePort; + std::string jsAppPath; + std::string bundleName; + std::string urlPath; + std::atomic isFinished; + std::atomic isRunning; + bool isDebug; + uint16_t debugServerPort; + uint32_t jsHeapSize; + std::string colorMode; + std::string orientation; + std::string aceVersion; + std::string screenDensity; + std::string configChanges; +}; +static std::vector liteDevice = {"liteWearable", "smartVision"}; + +#endif // JSAPP_H diff --git a/jsapp/lite/JsAppImpl.cpp b/jsapp/lite/JsAppImpl.cpp new file mode 100644 index 0000000..ed9b61b --- /dev/null +++ b/jsapp/lite/JsAppImpl.cpp @@ -0,0 +1,289 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "JsAppImpl.h" + +#include "engines/gfx/gfx_engine_manager.h" +#include "font/ui_font.h" +#include "font/ui_font_bitmap.h" +#include "font/ui_font_header.h" +#include "font/ui_font_vector.h" +#include "font/ui_line_break.h" +#include "font/ui_text_shaping.h" +#include "gfx_utils/color.h" +#include "gfx_utils/file.h" +#include "global.h" +#include "graphic_startup.h" +#include "input_device_manager.h" +#include "js_debugger_config.h" +#include "product_adapter.h" +#include "screen_device.h" + +#include "AsyncWorkManager.h" +#include "CommandParser.h" +#include "CppTimerManager.h" +#include "FileSystem.h" +#include "LanguageManagerImpl.h" +#include "MouseInputImpl.h" +#include "MouseWheelImpl.h" +#include "PreviewerEngineLog.h" +#include "SharedData.h" +#include "TimerTaskHandler.h" +#include "TraceTool.h" +#include "VirtualScreenImpl.h" +#include "json.h" +#include "ui_text_language.h" + +using namespace OHOS; +using namespace ACELite; +using namespace std; + +static uint8_t g_fontPsramBaseAddr[MIN_FONT_PSRAM_LENGTH]; +#if defined(LITEWEARABLE_SUPPORTED) +static uint8_t g_shapePsramBaseAddr[MIN_SHAPING_PSRAM_LENGTH]; +#endif +static void InitSmartVisionFont(UIFont* font, const string fontPath) +{ + ProductAdapter::SetDefaultFontStyle("SourceHanSansSC-Regular.otf", JsAppImpl::FONT_SIZE_DEFAULT); + BaseFont* currentFont = new UIFontVector(); + if (currentFont == nullptr) { + FLOG("InitSmartVisionFont currentFont memory allocation failed"); + return; + } + font->SetFont(currentFont); + font->SetPsramMemory(reinterpret_cast(g_fontPsramBaseAddr), MIN_FONT_PSRAM_LENGTH); + int8_t ret = font->SetFontPath(fontPath.data(), BaseFont::DYNAMIC_FONT); + if (ret != 0) { + ELOG("The vector font path does not exist ! fontPath : %s", fontPath.data()); + FLOG("InitFontEngine SetFontPath failed. vector fontPath: %s", fontPath.data()); + } + font->RegisterFontInfo("SourceHanSansSC-Regular.otf"); + font->RegisterFontInfo("HYQiHei-65S.otf"); + font->RegisterFontInfo("RobotoCondensed-Regular.ttf"); +} + +static void InitLiteWearable(UIFont* font, const string fontPath) +{ + ProductAdapter::SetDefaultFontStyle("HYQiHei-65S", JsAppImpl::FONT_SIZE_DEFAULT); + BitmapFontInit(); + BaseFont* currentFont = new UIFontBitmap(); + if (currentFont == nullptr) { + FLOG("InitLiteWearable currentFont memory allocation failed"); + return; + } + font->SetFont(currentFont); + font->SetPsramMemory(reinterpret_cast(g_fontPsramBaseAddr), MIN_FONT_PSRAM_LENGTH); + int8_t retDynamic = font->SetFontPath(string(fontPath + "font.bin").data(), BaseFont::DYNAMIC_FONT); + int8_t retStatic = font->SetFontPath(string(fontPath + "glyphs.bin").data(), BaseFont::STATIC_FONT); + if ((retDynamic != 0) || (retStatic != 0)) { + ELOG("The bitmap font path does not exist ! bitmap fontPath : %s", + string(fontPath + "font.bin").data()); + FLOG("InitFontEngine SetFontPath failed. bitmap fontPath: %s", string(fontPath + "font.bin").data()); + } + if (SharedData::GetData(LANGUAGE) == "zh-CN") { + font->SetCurrentLangId(LANGUAGE_CH); + } + if (SharedData::GetData(LANGUAGE) == "en-US") { + font->SetCurrentLangId(LANGUAGE_GB); + } +} + +static void InitFontEngine() +{ + UIFont* font = UIFont::GetInstance(); + if (font == nullptr) { + ELOG("InitFontEngine:font is nullptr"); + return; + } + std::string deviceType = CommandParser::GetInstance().GetDeviceType(); + std::string separator = FileSystem::GetSeparator(); + std::string fontPath = FileSystem::GetApplicationPath() + separator + ".." + separator + "config" + separator; + if (deviceType == "smartVision") { + InitSmartVisionFont(font, fontPath); + } + if (deviceType == "liteWearable") { + InitLiteWearable(font, fontPath); + } + +#if defined(LITEWEARABLE_SUPPORTED) + UITextShaping* pShaping = UITextShaping::GetInstance(); + int8_t ret = pShaping->SetPsramMemory(reinterpret_cast(g_shapePsramBaseAddr), MIN_SHAPING_PSRAM_LENGTH); + if (ret != INVALID_RET_VALUE) { + int32_t fp = 0; + string fileName = fontPath + "line_cj.brk"; +#ifdef _WIN32 + fp = open(fileName.c_str(), O_RDONLY | O_BINARY); +#else + fp = open(fileName.c_str(), O_RDONLY); +#endif + if (fp < 0) { + ELOG("Open font path failed."); + return; + } + uint32_t lineBrkSize = lseek(fp, 0, SEEK_END); + lseek(fp, 0, SEEK_SET); + UILineBreakEngine& lbEngine = UILineBreakEngine::GetInstance(); + lbEngine.SetRuleBinInfo(fp, 0, lineBrkSize); + lbEngine.Init(); + } +#endif +} + +static void InitHalScreen() +{ + InputDeviceManager::GetInstance()->Add(&MouseInputImpl::GetInstance()); + InputDeviceManager::GetInstance()->Add(&MouseWheelImpl::GetInstance()); +} + +JsAppImpl::JsAppImpl() + : isInterrupt(false), + taskHandleTimer(nullptr), + deviceCheckTimer(nullptr), + jsCheckTimer(nullptr), + jsAbility(nullptr), + jsThread(nullptr) +{ +} + +JsAppImpl::~JsAppImpl() {} + +JsAppImpl& JsAppImpl::GetInstance() +{ + static JsAppImpl instance; + return instance; +} + +void JsAppImpl::Start() +{ + if (jsThread != nullptr) { + delete jsThread; + jsThread = nullptr; + } + + isFinished = false; + isInterrupt = false; + jsThread = new thread(&JsAppImpl::ThreadCallBack, &JsAppImpl::GetInstance()); + if (jsThread == nullptr) { + ELOG("JsApp::Start jsThread memory allocation failed"); + } + jsThread->detach(); +} + +void JsAppImpl::Restart() +{ + Stop(); + Start(); +} + +void JsAppImpl::Interrupt() +{ + AsyncWorkManager::GetInstance().ClearAllAsyncWork(); + jsAbility->Hide(); + jsAbility->TransferToDestroy(); + jsAbility.reset(); + isFinished = true; + isInterrupt = true; + ILOG("JsAppImpl::ThreadCallBack finished"); +} + +void JsAppImpl::ThreadCallBack() +{ + OHOS::GraphicStartUp::Init(); + GLOBAL_ConfigLanguage(SharedData::GetData(LANGUAGE).data()); + InitHalScreen(); + InitFontEngine(); + VirtualScreenImpl::GetInstance().InitAll(pipeName, pipePort); + StartJsApp(); + InitTimer(); + + thread::id curThreadId = this_thread::get_id(); + SharedData::AppendNotify(LANGUAGE, TimerTaskHandler::CheckLanguageChanged, curThreadId); + + CppTimerManager& manager = CppTimerManager::GetTimerManager(); + while (!isInterrupt) { + this_thread::sleep_for(chrono::milliseconds(1)); + manager.RunTimerTick(); + } +} + +void JsAppImpl::InitTimer() +{ + taskHandleTimer = make_unique(TimerTaskHandler::TaskHandle); + if (taskHandleTimer == nullptr) { + ELOG("JsApp::InitTimer taskHandleTimer memory allocation failed."); + return; + } + CppTimerManager::GetTimerManager().AddCppTimer(*taskHandleTimer); + taskHandleTimer->Start(TASK_HANDLE_TIMER_INTERVAL); + + deviceCheckTimer = make_unique(TimerTaskHandler::CheckDevice); + if (deviceCheckTimer == nullptr) { + ELOG("JsApp::InitTimer deviceCheckTimer memory allocation failed."); + return; + } + CppTimerManager::GetTimerManager().AddCppTimer(*deviceCheckTimer); + deviceCheckTimer->Start(DEVICE_CHECK_TIMER_INTERVAL); + + jsCheckTimer = make_unique(TimerTaskHandler::CheckJsRunning); + if (jsCheckTimer == nullptr) { + ELOG("JsApp::InitTimer jsCheckTimer memory allocation failed."); + return; + } + CppTimerManager::GetTimerManager().AddCppTimer(*jsCheckTimer); + jsCheckTimer->Start(JS_CHECK_TIMER_INTERVAL); +} + +void JsAppImpl::StartJsApp() +{ + if (jsAbility != nullptr) { + FLOG("JsApp::StartJsApp jsAbility is not null."); + return; + } + + jsAbility = make_unique(); + if (jsAbility == nullptr) { + FLOG("JsApp::StartJsApp jsAbility memory allocation failed"); + return; + } + + DebuggerConfig config; + config.startDebuggerServer = isDebug; + ILOG("Launch JS APP."); + ILOG("Debug Server Enable: %d", config.startDebuggerServer); + config.snapshotMode = false; + config.heapSize = jsHeapSize; + if (isDebug && debugServerPort) { + config.port = debugServerPort; + config.startDebuggerServer = isDebug; + config.snapshotMode = false; + ILOG("Debug Server Port: %d", debugServerPort); + } + Debugger::GetInstance().ConfigEngineDebugger(config); + ILOG("Launch Js app"); + TraceTool::GetInstance().HandleTrace("Launch Js app"); + if (urlPath.empty()) { + jsAbility->Launch(jsAppPath.c_str(), bundleName.c_str(), 0); + jsAbility->Show(); + ILOG("JsApp::StartJsApp launch finished."); + return; + } + Json::Value val; + val["uri"] = urlPath; + string routerInfo = val.toStyledString(); + jsAbility->Launch(jsAppPath.c_str(), bundleName.c_str(), 0, routerInfo.data()); + jsAbility->Show(); + ILOG("JsApp::StartJsApp launch with single page mode finished."); + isFinished = false; +} diff --git a/jsapp/lite/JsAppImpl.h b/jsapp/lite/JsAppImpl.h new file mode 100644 index 0000000..5ddf66d --- /dev/null +++ b/jsapp/lite/JsAppImpl.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef JSAPPIMPL_H +#define JSAPPIMPL_H + +#include +#include + +#include "CppTimer.h" +#include "JsApp.h" +#include "js_ability.h" + +class JsAppImpl : public JsApp { +public: + JsAppImpl(); + virtual ~JsAppImpl(); + JsAppImpl& operator=(const JsAppImpl&) = delete; + JsAppImpl(const JsAppImpl&) = delete; + + static JsAppImpl& GetInstance(); + void Start() override; + void Restart() override; + void Interrupt() override; + static const uint8_t FONT_SIZE_DEFAULT = 30; + +private: + std::atomic isInterrupt; + const long TASK_HANDLE_TIMER_INTERVAL = 15; + const long DEVICE_CHECK_TIMER_INTERVAL = 100; + const long JS_CHECK_TIMER_INTERVAL = 1000; + std::unique_ptr taskHandleTimer; + std::unique_ptr deviceCheckTimer; + std::unique_ptr jsCheckTimer; + std::unique_ptr jsAbility; + std::thread* jsThread; + + void ThreadCallBack(); + void InitTimer(); + void StartJsApp(); +}; + +#endif // JSAPPIMPL_H diff --git a/jsapp/lite/TimerTaskHandler.cpp b/jsapp/lite/TimerTaskHandler.cpp new file mode 100644 index 0000000..087dbef --- /dev/null +++ b/jsapp/lite/TimerTaskHandler.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "TimerTaskHandler.h" + +#if defined(LITEWEARABLE_SUPPORTED) +#include "CallbackManager.h" +#endif + +#include "font/ui_font.h" +#include "global.h" +#include "json.h" +#include "task_manager.h" + +#include "AsyncWorkManager.h" +#include "CommandLineInterface.h" +#include "JsAppImpl.h" +#include "LanguageManagerImpl.h" +#include "PreviewerEngineLog.h" +#include "SharedDataManager.h" + +#if defined(LITEWEARABLE_SUPPORTED) +#include "VirtualLocation.h" +#endif + +#include "VirtualScreenImpl.h" + +using namespace OHOS::ACELite; +using namespace std; + +void TimerTaskHandler::TaskHandle() +{ + OHOS::TaskManager::GetInstance()->TaskHandler(); + VirtualScreenImpl::CheckBufferSend(); +} + +void TimerTaskHandler::CheckDevice() +{ + SharedDataManager::CheckTick(); + +#if defined(LITEWEARABLE_SUPPORTED) + if (VirtualLocation::GetInstance().IsPostionChanged()) { + VirtualLocation::GetInstance().ExecCallBack(); + } +#endif + + AsyncWorkManager::GetInstance().ExecAllAsyncWork(); +} + +#if defined(LITEWEARABLE_SUPPORTED) +void TimerTaskHandler::CheckBarometerChanged(uint32_t value) +{ + ILOG("CheckBarometerChanged value: %d", value); + CallbackManager::GetInstance().ExecuteNumberInfoSuccess(SIMULATOR_JS_ATAPTER_SUBSCRIBE_BAROMETER, "pressure", + value); +} + +void TimerTaskHandler::CheckStepCountChanged(uint32_t value) +{ + ILOG("CheckStepCountChanged value: %d", value); + CallbackManager::GetInstance().ExecuteNumberInfoSuccess(SIMULATOR_JS_ATAPTER_SUBSCRIBE_STEP_COUNTER, "steps", + value); +} + +void TimerTaskHandler::CheckHeartRateChanged(uint8_t value) +{ + ILOG("CheckHeartRateChanged value: %d", value); + CallbackManager::GetInstance().ExecuteNumberInfoSuccess(SIMULATOR_JS_ATAPTER_SUBSCRIBE_HEART_RATE, "heartRate", + value); +} + +void TimerTaskHandler::CheckOnBodyStateChanged(bool value) +{ + ILOG("CheckOnBodyStateChanged value: %d", value); + CallbackManager::GetInstance().ExecuteBooleanInfoSuccess(SIMULATOR_JS_ATAPTER_SUBSCRIBE_ON_BODY_STATE, "value", + value); +} +#endif + +void TimerTaskHandler::CheckLanguageChanged(std::string language) +{ + ILOG("CheckLanguageChanged value: %s", language.c_str()); + GLOBAL_ConfigLanguage(language.data()); +} + +void TimerTaskHandler::CheckBrightnessValueChanged(uint8_t value) +{ + Json::Value result; + result["version"] = CommandLineInterface::COMMAND_VERSION; + result["property"] = "Brightness"; + result["result"]["Brightness"] = value; + ILOG("CheckBrightnessValueChanged send Brightness: %d", static_cast(value)); + CommandLineInterface::SendJsonData(result); +} + +void TimerTaskHandler::CheckJsRunning() +{ + JsAppImpl::GetInstance().SetRunning(true); +} diff --git a/jsapp/lite/TimerTaskHandler.h b/jsapp/lite/TimerTaskHandler.h new file mode 100644 index 0000000..623d190 --- /dev/null +++ b/jsapp/lite/TimerTaskHandler.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TIMERTASKHANDLER_H +#define TIMERTASKHANDLER_H + +#include +#include + +namespace TimerTaskHandler { +void TaskHandle(); +void CheckDevice(); +void CheckBarometerChanged(uint32_t value); +void CheckStepCountChanged(uint32_t value); +void CheckHeartRateChanged(uint8_t value); +void CheckOnBodyStateChanged(bool value); +void CheckLanguageChanged(std::string language); +void CheckBrightnessValueChanged(uint8_t value); +void CheckJsRunning(); +}; // namespace TimerTaskHandler + +#endif // TIMERTASKHANDLER_H