From 56b6adb639f5ce6e6b300189e69eca5952f05e07 Mon Sep 17 00:00:00 2001 From: z00378708 Date: Wed, 4 Oct 2023 08:48:13 +0000 Subject: [PATCH] feat: add parameter continuous for set brightness Signed-off-by: z00378708 --- frameworks/napi/brightness.cpp | 41 +++++++++++-------- frameworks/napi/brightness.h | 4 +- frameworks/napi/brightness_module.cpp | 4 +- .../native/display_power_mgr_client.cpp | 4 +- .../native/include/display_power_mgr_client.h | 2 +- .../native/include/idisplay_power_mgr.h | 2 +- .../include/display_power_mgr_service.h | 2 +- service/native/include/screen_controller.h | 4 +- .../native/src/display_power_mgr_service.cpp | 7 ++-- service/native/src/screen_controller.cpp | 4 +- .../zidl/include/display_power_mgr_proxy.h | 2 +- service/zidl/src/display_power_mgr_proxy.cpp | 3 +- service/zidl/src/display_power_mgr_stub.cpp | 4 +- .../src/mock/display_mock_object_test.cpp | 2 +- .../src/mock/display_mock_parcel_test.cpp | 2 +- test/unittest/src/mock/display_mock_test.cpp | 2 +- 16 files changed, 51 insertions(+), 38 deletions(-) diff --git a/frameworks/napi/brightness.cpp b/frameworks/napi/brightness.cpp index 976d2cb..f862815 100644 --- a/frameworks/napi/brightness.cpp +++ b/frameworks/napi/brightness.cpp @@ -24,8 +24,11 @@ using namespace OHOS::PowerMgr; namespace OHOS { namespace DisplayPowerMgr { namespace { -const uint32_t MAX_ARGC = 1; -const uint32_t ARGV_ONE = 0; +const uint32_t MAX_ARGC = 2; +const uint32_t ARGV_BRIGHTNESS_INDEX = 0; +const uint32_t ARGV_CONTINUOUS_INDEX = 1; +const uint32_t ERR_DATA_INDEX = 0; +const uint32_t ERR_CODE_INDEX = 1; const uint32_t MAX_FAIL_ARGC = 2; const uint32_t MAX_BRIGHTNESS = DisplayPowerMgrClient::GetInstance().GetMaxBrightness(); const uint32_t MIN_BRIGHTNESS = DisplayPowerMgrClient::GetInstance().GetMinBrightness(); @@ -76,15 +79,16 @@ void Brightness::GetValue() void Brightness::SetValue(napi_callback_info& info) { DISPLAY_HILOGD(FEAT_BRIGHTNESS, "Brightness interface"); - napi_value napiBrightness = GetCallbackInfo(info, napi_number); - napi_value napiUndefined = GetCallbackInfo(info, napi_undefined); + napi_value napiBrightness = GetCallbackInfo(info, ARGV_BRIGHTNESS_INDEX, napi_number); + napi_value napiUndefined = GetCallbackInfo(info, ARGV_BRIGHTNESS_INDEX, napi_undefined); if (napiBrightness == nullptr && napiUndefined == nullptr) { result_.ThrowError(env_, DisplayErrors::ERR_PARAM_INVALID); return; } int32_t value = MIN_BRIGHTNESS; - if (napi_ok != napi_get_value_int32(env_, napiBrightness, &value)) { + bool continuous = false; + if (napi_get_value_int32(env_, napiBrightness, &value) != napi_ok) { if (napiUndefined != nullptr) { return; } else { @@ -93,7 +97,12 @@ void Brightness::SetValue(napi_callback_info& info) return; } } - if (!brightnessInfo_.SetBrightness(value)) { + napi_value napiContinuous = GetCallbackInfo(info, ARGV_CONTINUOUS_INDEX, napi_boolean); + if (napiContinuous != nullptr) { + napi_get_value_bool(env_, napiContinuous, &continuous); + } + + if (!brightnessInfo_.SetBrightness(value, continuous)) { DisplayErrors error = brightnessInfo_.GetServiceError(); if (error != DisplayErrors::ERR_OK) { result_.ThrowError(env_, error); @@ -111,7 +120,7 @@ void Brightness::SystemSetValue() napi_value napiVal = nullptr; napi_get_reference_value(env_, napiValRef_, &napiVal); napi_get_value_int32(env_, napiVal, &brightness); - brightnessInfo_.SetBrightness(brightness); + brightnessInfo_.SetBrightness(brightness, false); ReleaseReference(napiValRef_); } ExecuteCallback(); @@ -158,7 +167,7 @@ void Brightness::SetKeepScreenOn() ExecuteCallback(); } -napi_value Brightness::GetCallbackInfo(napi_callback_info& info, napi_valuetype checkType) +napi_value Brightness::GetCallbackInfo(napi_callback_info& info, uint32_t index, napi_valuetype checkType) { size_t argc = MAX_ARGC; napi_value argv[argc]; @@ -169,12 +178,12 @@ napi_value Brightness::GetCallbackInfo(napi_callback_info& info, napi_valuetype return nullptr; } - if (argc != MAX_ARGC) { - DISPLAY_HILOGW(COMP_FWK, "Lack of parameter"); + if (argc > MAX_ARGC || index >= argc) { + DISPLAY_HILOGW(COMP_FWK, "parameter %{pulic}u is invalid", index); return nullptr; } - napi_value options = argv[ARGV_ONE]; + napi_value options = argv[index]; RETURN_IF_WITH_RET(!CheckValueType(options, checkType), nullptr); return options; } @@ -226,8 +235,8 @@ void Brightness::Result::GetError(napi_env env, napi_value* error, size_t& size) napi_create_string_utf8(env, msg_.c_str(), msg_.size(), &data); napi_create_int32(env, code_, &code); size = MAX_FAIL_ARGC; - error[ARGV_ONE] = data; - error[MAX_ARGC] = code; + error[ERR_DATA_INDEX] = data; + error[ERR_CODE_INDEX] = code; } napi_value Brightness::Result::GetError(napi_env& env) @@ -285,12 +294,12 @@ uint32_t Brightness::BrightnessInfo::GetBrightness() const return brightness; } -bool Brightness::BrightnessInfo::SetBrightness(int32_t value) +bool Brightness::BrightnessInfo::SetBrightness(int32_t value, bool continuous) { DISPLAY_HILOGI(FEAT_BRIGHTNESS, "Set brightness: %{public}d", value); value = value > static_cast(MAX_BRIGHTNESS) ? static_cast(MAX_BRIGHTNESS) : value; value = value < static_cast(MIN_BRIGHTNESS) ? static_cast(MIN_BRIGHTNESS) : value; - bool isSucc = DisplayPowerMgrClient::GetInstance().SetBrightness(value); + bool isSucc = DisplayPowerMgrClient::GetInstance().SetBrightness(value, 0, continuous); if (!isSucc) { DISPLAY_HILOGW(FEAT_BRIGHTNESS, "Failed to set brightness: %{public}d", value); } @@ -394,4 +403,4 @@ void Brightness::ReleaseReference(napi_ref& ref) } } } // namespace DisplayPowerMgr -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/frameworks/napi/brightness.h b/frameworks/napi/brightness.h index 7de3a8a..04b3d5b 100644 --- a/frameworks/napi/brightness.h +++ b/frameworks/napi/brightness.h @@ -42,7 +42,7 @@ public: void GetMode(); void SetMode(); void SetKeepScreenOn(); - napi_value GetCallbackInfo(napi_callback_info& info, napi_valuetype checkType); + napi_value GetCallbackInfo(napi_callback_info& info, uint32_t index, napi_valuetype checkType); bool CreateCallbackRef(napi_value& options); void CreateValueRef(napi_value& options, const std::string& valName, napi_valuetype checkType); @@ -79,7 +79,7 @@ private: class BrightnessInfo { public: uint32_t GetBrightness() const; - bool SetBrightness(int32_t value); + bool SetBrightness(int32_t value, bool continuous); int32_t GetAutoMode() const; bool SetAutoMode(bool mode); void ScreenOn(bool keep, std::shared_ptr& runningLock); diff --git a/frameworks/napi/brightness_module.cpp b/frameworks/napi/brightness_module.cpp index 5dcc5cb..80b85c2 100644 --- a/frameworks/napi/brightness_module.cpp +++ b/frameworks/napi/brightness_module.cpp @@ -46,7 +46,7 @@ static napi_value SyncWork(napi_env env, const std::string resName, const std::s napi_get_undefined(env, &undefined); std::unique_ptr asyncBrightness = std::make_unique(env); RETURN_IF_WITH_RET(asyncBrightness == nullptr, undefined); - napi_value options = asyncBrightness->GetCallbackInfo(info, napi_object); + napi_value options = asyncBrightness->GetCallbackInfo(info, 0, napi_object); RETURN_IF_WITH_RET(options == nullptr, undefined); RETURN_IF_WITH_RET(!asyncBrightness->CreateCallbackRef(options), undefined); if (!valName.empty()) { @@ -151,7 +151,7 @@ static napi_value SetKeepScreenOn(napi_env env, napi_callback_info info) { std::unique_ptr asyncBrightness = std::make_unique(env, runningLock); RETURN_IF_WITH_RET(asyncBrightness == nullptr, nullptr); - napi_value options = asyncBrightness->GetCallbackInfo(info, napi_object); + napi_value options = asyncBrightness->GetCallbackInfo(info, 0, napi_object); RETURN_IF_WITH_RET(options == nullptr, nullptr); RETURN_IF_WITH_RET(!asyncBrightness->CreateCallbackRef(options), nullptr); asyncBrightness->CreateValueRef(options, Brightness::KEEP_SCREENON, napi_boolean); diff --git a/frameworks/native/display_power_mgr_client.cpp b/frameworks/native/display_power_mgr_client.cpp index f87d0e2..56c2385 100644 --- a/frameworks/native/display_power_mgr_client.cpp +++ b/frameworks/native/display_power_mgr_client.cpp @@ -116,11 +116,11 @@ int32_t DisplayPowerMgrClient::GetMainDisplayId() return static_cast(proxy->GetMainDisplayId()); } -bool DisplayPowerMgrClient::SetBrightness(uint32_t value, uint32_t displayId) +bool DisplayPowerMgrClient::SetBrightness(uint32_t value, uint32_t displayId, bool continuous) { auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, false); - return proxy->SetBrightness(value, displayId); + return proxy->SetBrightness(value, displayId, continuous); } bool DisplayPowerMgrClient::DiscountBrightness(double discount, uint32_t displayId) diff --git a/interfaces/inner_api/native/include/display_power_mgr_client.h b/interfaces/inner_api/native/include/display_power_mgr_client.h index 2a9fc14..90b32e5 100644 --- a/interfaces/inner_api/native/include/display_power_mgr_client.h +++ b/interfaces/inner_api/native/include/display_power_mgr_client.h @@ -37,7 +37,7 @@ public: DisplayState GetDisplayState(uint32_t id = 0); std::vector GetDisplayIds(); int32_t GetMainDisplayId(); - bool SetBrightness(uint32_t value, uint32_t displayId = 0); + bool SetBrightness(uint32_t value, uint32_t displayId = 0, bool continuous = false); bool DiscountBrightness(double discount, uint32_t displayId = 0); bool OverrideBrightness(uint32_t value, uint32_t displayId = 0); bool OverrideDisplayOffDelay(uint32_t delayMs); diff --git a/interfaces/inner_api/native/include/idisplay_power_mgr.h b/interfaces/inner_api/native/include/idisplay_power_mgr.h index 7eb4d6c..3a7659f 100644 --- a/interfaces/inner_api/native/include/idisplay_power_mgr.h +++ b/interfaces/inner_api/native/include/idisplay_power_mgr.h @@ -31,7 +31,7 @@ public: virtual DisplayState GetDisplayState(uint32_t id) = 0; virtual std::vector GetDisplayIds() = 0; virtual uint32_t GetMainDisplayId() = 0; - virtual bool SetBrightness(uint32_t value, uint32_t displayId) = 0; + virtual bool SetBrightness(uint32_t value, uint32_t displayId, bool continuous) = 0; virtual bool DiscountBrightness(double discount, uint32_t displayId) = 0; virtual bool OverrideBrightness(uint32_t value, uint32_t displayId) = 0; virtual bool OverrideDisplayOffDelay(uint32_t delayMs) = 0; diff --git a/service/native/include/display_power_mgr_service.h b/service/native/include/display_power_mgr_service.h index 4245f5c..db521c9 100644 --- a/service/native/include/display_power_mgr_service.h +++ b/service/native/include/display_power_mgr_service.h @@ -46,7 +46,7 @@ public: virtual DisplayState GetDisplayState(uint32_t id) override; virtual std::vector GetDisplayIds() override; virtual uint32_t GetMainDisplayId() override; - virtual bool SetBrightness(uint32_t value, uint32_t displayId) override; + virtual bool SetBrightness(uint32_t value, uint32_t displayId, bool continuous = false) override; virtual bool DiscountBrightness(double discount, uint32_t displayId) override; virtual bool OverrideBrightness(uint32_t value, uint32_t displayId) override; virtual bool OverrideDisplayOffDelay(uint32_t delayMs) override; diff --git a/service/native/include/screen_controller.h b/service/native/include/screen_controller.h index 1e0974f..b15a533 100644 --- a/service/native/include/screen_controller.h +++ b/service/native/include/screen_controller.h @@ -54,7 +54,7 @@ public: bool UpdateState(DisplayState state); bool IsScreenOn(); - bool SetBrightness(uint32_t value, uint32_t gradualDuration = 0); + bool SetBrightness(uint32_t value, uint32_t gradualDuration = 0, bool continuous = false); uint32_t GetBrightness(); uint32_t GetDeviceBrightness(); uint32_t GetCachedSettingBrightness() const; @@ -103,4 +103,4 @@ private: }; } // namespace DisplayPowerMgr } // namespace OHOS -#endif // DISPLAYMGR_SCREEN_CONTROLLER_H \ No newline at end of file +#endif // DISPLAYMGR_SCREEN_CONTROLLER_H diff --git a/service/native/src/display_power_mgr_service.cpp b/service/native/src/display_power_mgr_service.cpp index cc3cb3b..d1a2a14 100644 --- a/service/native/src/display_power_mgr_service.cpp +++ b/service/native/src/display_power_mgr_service.cpp @@ -230,7 +230,7 @@ uint32_t DisplayPowerMgrService::GetMainDisplayId() return id; } -bool DisplayPowerMgrService::SetBrightness(uint32_t value, uint32_t displayId) +bool DisplayPowerMgrService::SetBrightness(uint32_t value, uint32_t displayId, bool continuous) { if (!Permission::IsSystem()) { lastError_ = DisplayErrors::ERR_SYSTEM_API_DENIED; @@ -238,12 +238,13 @@ bool DisplayPowerMgrService::SetBrightness(uint32_t value, uint32_t displayId) } auto brightness = GetSafeBrightness(value); - DISPLAY_HILOGI(FEAT_BRIGHTNESS, "SetBrightness displayId=%{public}u, value=%{public}u", displayId, brightness); + DISPLAY_HILOGI(FEAT_BRIGHTNESS, "SetBrightness displayId=%{public}u, value=%{public}u, continuous=%{public}d", + displayId, brightness, continuous); auto iter = controllerMap_.find(displayId); if (iter == controllerMap_.end()) { return false; } - return iter->second->SetBrightness(brightness); + return iter->second->SetBrightness(brightness, 0, continuous); } bool DisplayPowerMgrService::DiscountBrightness(double discount, uint32_t displayId) diff --git a/service/native/src/screen_controller.cpp b/service/native/src/screen_controller.cpp index 6f8df55..bb30155 100644 --- a/service/native/src/screen_controller.cpp +++ b/service/native/src/screen_controller.cpp @@ -149,7 +149,7 @@ bool ScreenController::IsScreenOn() return (state_ == DisplayState::DISPLAY_ON || state_ == DisplayState::DISPLAY_DIM); } -bool ScreenController::SetBrightness(uint32_t value, uint32_t gradualDuration) +bool ScreenController::SetBrightness(uint32_t value, uint32_t gradualDuration, bool continuous) { if (!CanSetBrightness()) { DISPLAY_HILOGW(FEAT_BRIGHTNESS, "Cannot set brightness, ignore the change,"\ @@ -157,7 +157,7 @@ bool ScreenController::SetBrightness(uint32_t value, uint32_t gradualDuration) cachedSettingBrightness_ = value; return false; } - return UpdateBrightness(value, gradualDuration, true); + return UpdateBrightness(value, gradualDuration, !continuous); } uint32_t ScreenController::GetBrightness() diff --git a/service/zidl/include/display_power_mgr_proxy.h b/service/zidl/include/display_power_mgr_proxy.h index 469881d..24b6058 100644 --- a/service/zidl/include/display_power_mgr_proxy.h +++ b/service/zidl/include/display_power_mgr_proxy.h @@ -40,7 +40,7 @@ public: virtual std::vector GetDisplayIds() override; virtual uint32_t GetMainDisplayId() override; - virtual bool SetBrightness(uint32_t value, uint32_t displayId) override; + virtual bool SetBrightness(uint32_t value, uint32_t displayId, bool continuous) override; virtual bool DiscountBrightness(double value, uint32_t displayId) override; virtual bool OverrideBrightness(uint32_t value, uint32_t displayId) override; virtual bool OverrideDisplayOffDelay(uint32_t delayMs) override; diff --git a/service/zidl/src/display_power_mgr_proxy.cpp b/service/zidl/src/display_power_mgr_proxy.cpp index 84e0d6c..d5ab3d5 100644 --- a/service/zidl/src/display_power_mgr_proxy.cpp +++ b/service/zidl/src/display_power_mgr_proxy.cpp @@ -166,7 +166,7 @@ uint32_t DisplayPowerMgrProxy::GetMainDisplayId() return result; } -bool DisplayPowerMgrProxy::SetBrightness(uint32_t value, uint32_t displayId) +bool DisplayPowerMgrProxy::SetBrightness(uint32_t value, uint32_t displayId, bool continuous) { sptr remote = Remote(); RETURN_IF_WITH_RET(remote == nullptr, false); @@ -183,6 +183,7 @@ bool DisplayPowerMgrProxy::SetBrightness(uint32_t value, uint32_t displayId) WRITE_PARCEL_WITH_RET(data, Uint32, value, false); WRITE_PARCEL_WITH_RET(data, Uint32, displayId, false); + WRITE_PARCEL_WITH_RET(data, Bool, continuous, false); int ret = remote->SendRequest( static_cast(PowerMgr::DisplayPowerMgrInterfaceCode::SET_BRIGHTNESS), diff --git a/service/zidl/src/display_power_mgr_stub.cpp b/service/zidl/src/display_power_mgr_stub.cpp index 73744ed..69e7156 100644 --- a/service/zidl/src/display_power_mgr_stub.cpp +++ b/service/zidl/src/display_power_mgr_stub.cpp @@ -178,11 +178,13 @@ int32_t DisplayPowerMgrStub::SetBrightnessStub(MessageParcel& data, MessageParce { uint32_t value = 0; uint32_t displayId = 0; + bool continuous = false; READ_PARCEL_WITH_RET(data, Uint32, value, E_READ_PARCEL_ERROR); READ_PARCEL_WITH_RET(data, Uint32, displayId, E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Bool, continuous, E_READ_PARCEL_ERROR); - bool ret = SetBrightness(value, displayId); + bool ret = SetBrightness(value, displayId, continuous); if (!reply.WriteBool(ret)) { DISPLAY_HILOGE(COMP_SVC, "Failed to write SetBrightness return value"); return E_WRITE_PARCEL_ERROR; diff --git a/test/unittest/src/mock/display_mock_object_test.cpp b/test/unittest/src/mock/display_mock_object_test.cpp index 0cb4180..a16effb 100644 --- a/test/unittest/src/mock/display_mock_object_test.cpp +++ b/test/unittest/src/mock/display_mock_object_test.cpp @@ -62,7 +62,7 @@ HWTEST_F(DisplayMockObjectTest, DisplayMockObjectTest_001, TestSize.Level0) result.push_back(DISPLAY_ID); EXPECT_NE(result, sptrDisplayProxy->GetDisplayIds()); EXPECT_EQ(MAIN_ID_PROXY, sptrDisplayProxy->GetMainDisplayId()); - EXPECT_FALSE(sptrDisplayProxy->SetBrightness(BRIGHTNESS_SETTING_VALUE, DISPLAY_ID)); + EXPECT_FALSE(sptrDisplayProxy->SetBrightness(BRIGHTNESS_SETTING_VALUE, DISPLAY_ID, false)); EXPECT_FALSE(sptrDisplayProxy->DiscountBrightness(DISCOUNT_VALUE, DISPLAY_ID)); EXPECT_FALSE(sptrDisplayProxy->OverrideBrightness(BRIGHTNESS_OVERRIDE_VALUE, DISPLAY_ID)); EXPECT_FALSE(sptrDisplayProxy->RestoreBrightness(DISPLAY_ID)); diff --git a/test/unittest/src/mock/display_mock_parcel_test.cpp b/test/unittest/src/mock/display_mock_parcel_test.cpp index 08322a8..28c7850 100644 --- a/test/unittest/src/mock/display_mock_parcel_test.cpp +++ b/test/unittest/src/mock/display_mock_parcel_test.cpp @@ -56,7 +56,7 @@ void DisplayMockParcelTest::DisplayProxyTestFunc(std::shared_ptrGetDisplayState(DISPLAY_ID)); EXPECT_NE(result, sptrDisplayProxy->GetDisplayIds()); EXPECT_EQ(MAIN_ID_PROXY, sptrDisplayProxy->GetMainDisplayId()); - EXPECT_FALSE(sptrDisplayProxy->SetBrightness(BRIGHTNESS_SETTING_VALUE, DISPLAY_ID)); + EXPECT_FALSE(sptrDisplayProxy->SetBrightness(BRIGHTNESS_SETTING_VALUE, DISPLAY_ID, false)); EXPECT_FALSE(sptrDisplayProxy->DiscountBrightness(DISCOUNT_VALUE, DISPLAY_ID)); EXPECT_FALSE(sptrDisplayProxy->OverrideBrightness(BRIGHTNESS_OVERRIDE_VALUE, DISPLAY_ID)); EXPECT_FALSE(sptrDisplayProxy->RestoreBrightness(DISPLAY_ID)); diff --git a/test/unittest/src/mock/display_mock_test.cpp b/test/unittest/src/mock/display_mock_test.cpp index 50c2488..aef480f 100644 --- a/test/unittest/src/mock/display_mock_test.cpp +++ b/test/unittest/src/mock/display_mock_test.cpp @@ -99,7 +99,7 @@ HWTEST_F(DisplayMockTest, DisplayMockTest_003, TestSize.Level0) result.push_back(DISPLAY_ID); EXPECT_NE(result, sptrDisplayProxy->GetDisplayIds()); EXPECT_EQ(MAIN_ID, sptrDisplayProxy->GetMainDisplayId()); - EXPECT_FALSE(sptrDisplayProxy->SetBrightness(BRIGHTNESS_SETTING_VALUE, DISPLAY_ID)); + EXPECT_FALSE(sptrDisplayProxy->SetBrightness(BRIGHTNESS_SETTING_VALUE, DISPLAY_ID, false)); EXPECT_FALSE(sptrDisplayProxy->DiscountBrightness(DISCOUNT_VALUE, DISPLAY_ID)); EXPECT_FALSE(sptrDisplayProxy->OverrideBrightness(BRIGHTNESS_OVERRIDE_VALUE, DISPLAY_ID)); EXPECT_FALSE(sptrDisplayProxy->RestoreBrightness(DISPLAY_ID)); -- Gitee