diff --git a/interfaces/inner_api/security_component_common/sec_comp_info_helper.h b/interfaces/inner_api/security_component_common/sec_comp_info_helper.h index 7d5a44e3fef947a7431647a3a193e74cc4939bbd..5902c6f0c6e761e7fec3f53381777a06680f3a37 100644 --- a/interfaces/inner_api/security_component_common/sec_comp_info_helper.h +++ b/interfaces/inner_api/security_component_common/sec_comp_info_helper.h @@ -51,7 +51,7 @@ struct ScreenInfo { private: static float GetWindowScale(int32_t windowId); - static void AdjustSecCompRect(SecCompBase* comp, float scale); + static void AdjustSecCompRect(SecCompBase* comp, float scale, bool isCompatScaleMode); static bool IsOutOfWatchScreen(const SecCompRect& rect, double radius, std::string& message); static bool IsOutOfScreen(const SecCompRect& rect, double curScreenWidth, double curScreenHeight, std::string& message, bool isWearable); diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp index ae6624c88baa05a84343bd6cb7005408b0cb96ed..6c113a238fd63e7b4688bed4def0008a8625a5dc 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp @@ -46,16 +46,25 @@ const int NUMBER_TWO = 2; const char HEX_FILL_CHAR = '0'; } -void SecCompInfoHelper::AdjustSecCompRect(SecCompBase* comp, float scale) +void SecCompInfoHelper::AdjustSecCompRect(SecCompBase* comp, float scale, bool isCompatScaleMode) { comp->rect_.width_ *= scale; comp->rect_.height_ *= scale; - comp->rect_.x_ = comp->windowRect_.x_ + (comp->rect_.x_ - comp->windowRect_.x_) * scale; - comp->rect_.y_ = comp->windowRect_.y_ + (comp->rect_.y_ - comp->windowRect_.y_) * scale; - + if (!isCompatScaleMode) { + // window scales towards the top-left corner + comp->rect_.x_ = comp->windowRect_.x_ + (comp->rect_.x_ - comp->windowRect_.x_) * scale; + comp->rect_.y_ = comp->windowRect_.y_ + (comp->rect_.y_ - comp->windowRect_.y_) * scale; + } else { + // window scales towards the center + auto disX = comp->rect_.x_ - comp->windowRect_.x_; + auto disY = comp->rect_.y_ - comp->windowRect_.y_; + comp->windowRect_.x_ = comp->windowRect_.x_ + (1 - scale) * comp->windowRect_.width_ / NUMBER_TWO; + comp->windowRect_.y_ = comp->windowRect_.y_ + (1 - scale) * comp->windowRect_.height_ / NUMBER_TWO; + comp->rect_.x_ = comp->windowRect_.x_ + scale * disX; + comp->rect_.y_ = comp->windowRect_.y_ + scale * disY; + } SC_LOG_DEBUG(LABEL, "After adjust x %{public}f, y %{public}f, width %{public}f, height %{public}f", comp->rect_.x_, comp->rect_.y_, comp->rect_.width_, comp->rect_.height_); - comp->windowRect_.width_ *= scale; comp->windowRect_.height_ *= scale; } @@ -337,10 +346,11 @@ bool SecCompInfoHelper::CheckComponentValid(SecCompBase* comp, std::string& mess return false; } - float scale = WindowInfoHelper::GetWindowScale(comp->windowId_); + bool isCompatScaleMode = false; + float scale = WindowInfoHelper::GetWindowScale(comp->windowId_, isCompatScaleMode); SC_LOG_DEBUG(LABEL, "WindowScale = %{public}f", scale); if (!IsEqual(scale, WindowInfoHelper::FULL_SCREEN_SCALE) && !IsEqual(scale, 0.0)) { - AdjustSecCompRect(comp, scale); + AdjustSecCompRect(comp, scale, isCompatScaleMode); } if (!CheckSecCompBase(comp, message)) { diff --git a/services/security_component_service/sa/sa_main/window_info_helper.cpp b/services/security_component_service/sa/sa_main/window_info_helper.cpp index c119ccec18c5a9cf2ebe17386c2e99757fa797db..6c791ab9f7332f5103e2b126273ce93533a0be5e 100644 --- a/services/security_component_service/sa/sa_main/window_info_helper.cpp +++ b/services/security_component_service/sa/sa_main/window_info_helper.cpp @@ -28,7 +28,7 @@ constexpr int32_t INVALID_WINDOW_LAYER = -1; constexpr uint32_t UI_EXTENSION_MASK = 0x40000000; } -float WindowInfoHelper::GetWindowScale(int32_t windowId) +float WindowInfoHelper::GetWindowScale(int32_t windowId, bool& isCompatScaleMode) { float scale = FULL_SCREEN_SCALE; std::vector> infos; @@ -43,8 +43,9 @@ float WindowInfoHelper::GetWindowScale(int32_t windowId) SC_LOG_WARN(LABEL, "Cannot find AccessibilityWindowInfo, return default scale"); return scale; } + isCompatScaleMode = (*iter)->isCompatScaleMode_; scale = (*iter)->scaleVal_; - SC_LOG_INFO(LABEL, "Get scale %{public}f", scale); + SC_LOG_INFO(LABEL, "Get scale = %{public}f, isCompatScaleMode = %{public}d", scale, isCompatScaleMode); return scale; } diff --git a/services/security_component_service/sa/sa_main/window_info_helper.h b/services/security_component_service/sa/sa_main/window_info_helper.h index f1c088572f7eac23790351568253c5350c7423f7..aec50358690724b7af73b3f453f6b760d1c497dc 100644 --- a/services/security_component_service/sa/sa_main/window_info_helper.h +++ b/services/security_component_service/sa/sa_main/window_info_helper.h @@ -23,7 +23,7 @@ namespace Security { namespace SecurityComponent { class __attribute__((visibility("default"))) WindowInfoHelper { public: - static float GetWindowScale(int32_t windowId); + static float GetWindowScale(int32_t windowId, bool& isCompatScaleMode); static bool CheckOtherWindowCoverComp(int32_t compWinId, const SecCompRect& secRect, std::string& message); public: static constexpr float FULL_SCREEN_SCALE = 1.0F; diff --git a/services/security_component_service/sa/test/mock/include/window_manager.h b/services/security_component_service/sa/test/mock/include/window_manager.h index f7479c2660ad2d1e6f089f4c42c595d6a80077c7..c24499e5c771c988e5dc66e350eed8c4483c5121 100644 --- a/services/security_component_service/sa/test/mock/include/window_manager.h +++ b/services/security_component_service/sa/test/mock/include/window_manager.h @@ -55,6 +55,7 @@ public: WindowMode mode_; WindowType type_; float scaleVal_; + bool isCompatScaleMode_ { false }; }; class UnreliableWindowInfo : public Parcelable { diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index 679268f373bb9d818f8c2e27ac0b5e8283f8e983..60e2b315b9a7805fe61da52fda928a68d896e774 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -22,6 +22,7 @@ #include "location_button.h" #include "paste_button.h" #include "save_button.h" +#include "sec_comp_info.h" #include "sec_comp_info_helper.h" #include "sec_comp_log.h" #include "sec_comp_err.h" @@ -660,3 +661,48 @@ HWTEST_F(SecCompInfoHelperTest, IsColorSimilar001, TestSize.Level0) }; EXPECT_TRUE(IsColorSimilar(color1, color2)); } + +/** + * @tc.name: AdjustSecCompRect001 + * @tc.desc: Test AdjustSecCompRect + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(SecCompInfoHelperTest, AdjustSecCompRect001, TestSize.Level0) +{ + nlohmann::json jsonComponent; + ServiceTestCommon::BuildLocationComponentJson(jsonComponent); + std::string message; + SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message); + float scale = 0.8f; + + comp->rect_.x_ = 10.0f; + comp->rect_.y_ = 5.0f; + comp->rect_.width_ = 20.0f; + comp->rect_.height_ = 10.0f; + comp->windowRect_.x_ = 0.0f; + comp->windowRect_.y_ = 0.0f; + comp->windowRect_.width_ = 200.0f; + comp->windowRect_.height_ = 100.0f; + + SecCompInfoHelper::AdjustSecCompRect(comp, scale, false); + EXPECT_TRUE(IsEqual(comp->rect_.width_, 16)); + EXPECT_TRUE(IsEqual(comp->rect_.height_, 8)); + EXPECT_TRUE(IsEqual(comp->rect_.x_, 8)); + EXPECT_TRUE(IsEqual(comp->rect_.y_, 4)); + + comp->rect_.x_ = 10.0f; + comp->rect_.y_ = 5.0f; + comp->rect_.width_ = 20.0f; + comp->rect_.height_ = 10.0f; + comp->windowRect_.x_ = 0.0f; + comp->windowRect_.y_ = 0.0f; + comp->windowRect_.width_ = 200.0f; + comp->windowRect_.height_ = 100.0f; + + SecCompInfoHelper::AdjustSecCompRect(comp, scale, true); + EXPECT_TRUE(IsEqual(comp->rect_.width_, 16)); + EXPECT_TRUE(IsEqual(comp->rect_.height_, 8)); + EXPECT_TRUE(IsEqual(comp->rect_.x_, 28)); + EXPECT_TRUE(IsEqual(comp->rect_.y_, 14)); +} diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.h b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.h index a9d845964b1791d69009082178f2339c7c47b3de..7bd06cd81b945c7dc1bba1651a2fa43a48f139b3 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.h +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.h @@ -16,7 +16,9 @@ #define SEC_COMP_INFO_HELPER_TEST_H #include +#define private public #include "sec_comp_info_helper.h" +#undef private namespace OHOS { namespace Security {