C++侧沉浸式代码优化,

1、AvoidAreaType添加TYPE_START和TYPE_END枚举值用以标记避让区域头尾类型,便于条件循环,消除代码魔术值。
2、SceneSessionManager中,UpdateSessionAvoidAreaListener、UpdateSessionAvoidAreaIfNeed、UpdateNormalSessionAvoidArea函数参数消除数值引用

Signed-off-by: wangzilin <wangzilin17@huawei.com>
This commit is contained in:
wangzilin 2024-11-05 09:48:48 +08:00
parent 1c17f636f8
commit 88e823052a
10 changed files with 26 additions and 18 deletions

View File

@ -796,11 +796,13 @@ struct KeyboardPanelInfo : public Parcelable {
* @brief Enumerates avoid area type.
*/
enum class AvoidAreaType : uint32_t {
TYPE_START = -1,
TYPE_SYSTEM, // area of SystemUI
TYPE_CUTOUT, // cutout of screen
TYPE_SYSTEM_GESTURE, // area for system gesture
TYPE_KEYBOARD, // area for soft input keyboard
TYPE_NAVIGATION_INDICATOR, // area for navigation indicator
TYPE_END,
};
/**

View File

@ -402,7 +402,7 @@ int32_t CJWindowImpl::GetWindowAvoidArea(uint32_t areaType, CAvoidArea* retPtr)
return static_cast<int32_t>(WmErrorCode::WM_ERROR_STATE_ABNORMALLY);
}
AvoidAreaType avoidAreaType = static_cast<AvoidAreaType>(areaType);
if ((avoidAreaType > AvoidAreaType::TYPE_NAVIGATION_INDICATOR) || (avoidAreaType < AvoidAreaType::TYPE_SYSTEM)) {
if ((avoidAreaType >= AvoidAreaType::TYPE_END) || (avoidAreaType <= AvoidAreaType::TYPE_START)) {
return static_cast<int32_t>(WmErrorCode::WM_ERROR_INVALID_PARAM);
}
AvoidArea avoidArea;

View File

@ -668,8 +668,8 @@ napi_value JsExtensionWindow::OnGetWindowAvoidArea(napi_env env, napi_callback_i
uint32_t resultValue = 0;
napi_get_value_uint32(env, nativeMode, &resultValue);
avoidAreaType = static_cast<AvoidAreaType>(resultValue);
errCode = ((avoidAreaType > AvoidAreaType::TYPE_NAVIGATION_INDICATOR) ||
(avoidAreaType < AvoidAreaType::TYPE_SYSTEM)) ?
errCode = ((avoidAreaType >= AvoidAreaType::TYPE_END) ||
(avoidAreaType <= AvoidAreaType::TYPE_START)) ?
WmErrorCode::WM_ERROR_INVALID_PARAM : WmErrorCode::WM_OK;
}
if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {

View File

@ -3145,7 +3145,7 @@ static void ParseAvoidAreaParam(napi_env env, napi_callback_info info, WMError&
napi_get_value_uint32(env, nativeMode, &resultValue));
avoidAreaType = static_cast<AvoidAreaType>(resultValue);
errCode = ((avoidAreaType > AvoidAreaType::TYPE_KEYBOARD) ||
(avoidAreaType < AvoidAreaType::TYPE_SYSTEM)) ? WMError::WM_ERROR_INVALID_PARAM : WMError::WM_OK;
(avoidAreaType <= AvoidAreaType::TYPE_START)) ? WMError::WM_ERROR_INVALID_PARAM : WMError::WM_OK;
}
}
}
@ -3218,8 +3218,8 @@ napi_value JsWindow::OnGetWindowAvoidAreaSync(napi_env env, napi_callback_info i
CHECK_NAPI_RETCODE(errCode, WmErrorCode::WM_ERROR_INVALID_PARAM,
napi_get_value_uint32(env, nativeMode, &resultValue));
avoidAreaType = static_cast<AvoidAreaType>(resultValue);
errCode = ((avoidAreaType > AvoidAreaType::TYPE_NAVIGATION_INDICATOR) ||
(avoidAreaType < AvoidAreaType::TYPE_SYSTEM)) ?
errCode = ((avoidAreaType >= AvoidAreaType::TYPE_END) ||
(avoidAreaType <= AvoidAreaType::TYPE_START)) ?
WmErrorCode::WM_ERROR_INVALID_PARAM : WmErrorCode::WM_OK;
}
if (errCode == WmErrorCode::WM_ERROR_INVALID_PARAM) {

View File

@ -547,11 +547,13 @@ struct SystemBarProperty {
* @brief Enumerates avoid area type.
*/
enum class AvoidAreaType : uint32_t {
TYPE_START = -1,
TYPE_SYSTEM, // area of SystemUI
TYPE_CUTOUT, // cutout of screen
TYPE_SYSTEM_GESTURE, // area for system gesture
TYPE_KEYBOARD, // area for soft input keyboard
TYPE_NAVIGATION_INDICATOR, // area for navigation indicator
TYPE_END,
};
/**

View File

@ -312,8 +312,10 @@ int SessionStageStub::HandleUpdateAvoidArea(MessageParcel& data, MessageParcel&
if (!avoidArea) {
return ERR_INVALID_VALUE;
}
uint32_t type;
if (!data.ReadUint32(type)) {
uint32_t type = 0;
if (!data.ReadUint32(type) &&
type <= static_cast<uint32_t>(AvoidAreaType::TYPE_START) &&
type >= static_cast<uint32_t>(AvoidAreaType::TYPE_END)) {
return ERR_INVALID_VALUE;
}
UpdateAvoidArea(avoidArea, static_cast<AvoidAreaType>(type));

View File

@ -1867,7 +1867,7 @@ WSError SceneSession::GetAllAvoidAreas(std::map<AvoidAreaType, AvoidArea>& avoid
using T = std::underlying_type_t<AvoidAreaType>;
for (T avoidType = static_cast<T>(AvoidAreaType::TYPE_SYSTEM);
avoidType <= static_cast<T>(AvoidAreaType::TYPE_NAVIGATION_INDICATOR); avoidType++) {
avoidType < static_cast<T>(AvoidAreaType::TYPE_END; avoidType++) {
auto type = static_cast<AvoidAreaType>(avoidType);
avoidAreas[type] = session->GetAvoidAreaByTypeInner(type);
}

View File

@ -806,8 +806,8 @@ int SessionStub::HandleGetAvoidAreaByType(MessageParcel& data, MessageParcel& re
{
uint32_t typeId = 0;
if (!data.ReadUint32(typeId) ||
typeId < static_cast<uint32_t>(AvoidAreaType::TYPE_SYSTEM) ||
typeId > static_cast<uint32_t>(AvoidAreaType::TYPE_NAVIGATION_INDICATOR)) {
typeId <= static_cast<uint32_t>(AvoidAreaType::TYPE_START) ||
typeId >= static_cast<uint32_t>(AvoidAreaType::TYPE_END)) {
return ERR_INVALID_DATA;
}
AvoidAreaType type = static_cast<AvoidAreaType>(typeId);

View File

@ -8044,7 +8044,7 @@ WSError SceneSessionManager::GetFocusSessionElement(AppExecFwk::ElementName& ele
return taskScheduler_->PostSyncTask(task, "GetFocusSessionElement");
}
WSError SceneSessionManager::UpdateSessionAvoidAreaListener(int32_t& persistentId, bool haveListener)
WSError SceneSessionManager::UpdateSessionAvoidAreaListener(int32_t& persistentId, bool& haveListener)
{
const auto callingPid = IPCSkeleton::GetCallingRealPid();
auto task = [this, persistentId, haveListener, callingPid]() {
@ -8073,7 +8073,7 @@ WSError SceneSessionManager::UpdateSessionAvoidAreaListener(int32_t& persistentI
}
bool SceneSessionManager::UpdateSessionAvoidAreaIfNeed(const int32_t& persistentId,
const sptr<SceneSession>& sceneSession, const AvoidArea& avoidArea, AvoidAreaType avoidAreaType)
const sptr<SceneSession>& sceneSession, const AvoidArea& avoidArea, AvoidAreaType& avoidAreaType)
{
if (sceneSession == nullptr) {
TLOGI(WmsLogTag::WMS_IMMS, "scene session null no need update avoid area");
@ -8085,7 +8085,9 @@ bool SceneSessionManager::UpdateSessionAvoidAreaIfNeed(const int32_t& persistent
bool needUpdate = true;
if (auto iter = lastUpdatedAvoidArea_[persistentId].find(avoidAreaType);
iter != lastUpdatedAvoidArea_[persistentId].end()) {
iter != lastUpdatedAvoidArea_[persistentId].end() &&
iter != AvoidAreaType::TYPE_START &&
iter != AvoidAreaType::TYPE_END) {
needUpdate = iter->second != avoidArea;
} else {
if (avoidArea.isEmptyAvoidArea()) {
@ -8153,8 +8155,8 @@ void SceneSessionManager::UpdateNormalSessionAvoidArea(
return;
}
uint32_t start = static_cast<uint32_t>(AvoidAreaType::TYPE_SYSTEM);
uint32_t end = static_cast<uint32_t>(AvoidAreaType::TYPE_NAVIGATION_INDICATOR);
for (uint32_t avoidType = start; avoidType <= end; avoidType++) {
uint32_t end = static_cast<uint32_t>(AvoidAreaType::TYPE_END);
for (uint32_t avoidType = start; avoidType < end; avoidType++) {
AvoidArea avoidArea = sceneSession->GetAvoidAreaByType(static_cast<AvoidAreaType>(avoidType));
if (avoidType == static_cast<uint32_t>(AvoidAreaType::TYPE_NAVIGATION_INDICATOR) &&
!CheckAvoidAreaForAINavigationBar(isAINavigationBarVisible_, avoidArea,

View File

@ -103,8 +103,8 @@ int32_t WindowManagerStub::OnRemoteRequest(uint32_t code, MessageParcel& data, M
uint32_t windowId = data.ReadUint32();
uint32_t avoidAreaTypeId = 0;
if (!data.ReadUint32(avoidAreaTypeId) ||
avoidAreaTypeId < static_cast<uint32_t>(AvoidAreaType::TYPE_SYSTEM) ||
avoidAreaTypeId > static_cast<uint32_t>(AvoidAreaType::TYPE_NAVIGATION_INDICATOR)) {
avoidAreaTypeId <= static_cast<uint32_t>(AvoidAreaType::TYPE_START) ||
avoidAreaTypeId >= static_cast<uint32_t>(AvoidAreaType::TYPE_END)) {
return ERR_INVALID_DATA;
}
auto avoidAreaType = static_cast<AvoidAreaType>(avoidAreaTypeId);