diff --git a/dm/include/display_manager_adapter.h b/dm/include/display_manager_adapter.h index f206bcb9..e4403668 100644 --- a/dm/include/display_manager_adapter.h +++ b/dm/include/display_manager_adapter.h @@ -89,6 +89,8 @@ public: virtual bool SetScreenActiveMode(ScreenId screenId, uint32_t modeId); virtual sptr GetScreenInfo(ScreenId screenId); virtual bool SetVirtualPixelRatio(ScreenId screenId, float virtualPixelRatio); + virtual DMError SetScreenRotationLocked(bool isLocked); + virtual bool IsScreenRotationLocked(); // colorspace, gamut virtual DMError GetScreenSupportedColorGamuts(ScreenId screenId, std::vector& colorGamuts); diff --git a/dm/src/display_manager_adapter.cpp b/dm/src/display_manager_adapter.cpp index 2402ec2b..82cdda11 100644 --- a/dm/src/display_manager_adapter.cpp +++ b/dm/src/display_manager_adapter.cpp @@ -125,6 +125,20 @@ DMError ScreenManagerAdapter::SetVirtualScreenSurface(ScreenId screenId, sptrSetVirtualScreenSurface(screenId, surface); } +DMError ScreenManagerAdapter::SetScreenRotationLocked(bool isLocked) +{ + INIT_PROXY_CHECK_RETURN(DMError::DM_ERROR_INIT_DMS_PROXY_LOCKED); + WLOGFI("DisplayManagerAdapter::SetScreenRotationLocked"); + return displayManagerServiceProxy_->SetScreenRotationLocked(isLocked); +} + +bool ScreenManagerAdapter::IsScreenRotationLocked() +{ + INIT_PROXY_CHECK_RETURN(false); + WLOGFI("DisplayManagerAdapter::IsScreenRotationLocked"); + return displayManagerServiceProxy_->IsScreenRotationLocked(); +} + bool ScreenManagerAdapter::SetScreenPowerForAll(ScreenPowerState state, PowerStateChangeReason reason) { INIT_PROXY_CHECK_RETURN(false); diff --git a/dm/src/screen_manager.cpp b/dm/src/screen_manager.cpp index 5580390a..9f3d784f 100644 --- a/dm/src/screen_manager.cpp +++ b/dm/src/screen_manager.cpp @@ -435,6 +435,16 @@ ScreenPowerState ScreenManager::GetScreenPower(ScreenId dmsScreenId) return SingletonContainer::Get().GetScreenPower(dmsScreenId); } +DMError ScreenManager::SetScreenRotationLocked(bool isLocked) +{ + return SingletonContainer::Get().SetScreenRotationLocked(isLocked); +} + +bool ScreenManager::IsScreenRotationLocked() +{ + return SingletonContainer::Get().IsScreenRotationLocked(); +} + void ScreenManager::Impl::NotifyScreenConnect(sptr info) { std::lock_guard lock(mutex_); diff --git a/dm/test/systemtest/screen_manager_test.cpp b/dm/test/systemtest/screen_manager_test.cpp index d338001c..489a3ee8 100644 --- a/dm/test/systemtest/screen_manager_test.cpp +++ b/dm/test/systemtest/screen_manager_test.cpp @@ -743,6 +743,20 @@ HWTEST_F(ScreenManagerTest, ScreenManager17, Function | MediumTest | Level2) ASSERT_NE(SCREEN_ID_INVALID, virtualScreenId); } } + +/** + * @tc.name: ScreenManager18 + * @tc.desc: Set screen rotation lock, and check whether screen rotation lock is Locked. + * @tc.type: FUNC + */ +HWTEST_F(ScreenManagerTest, ScreenManager18, Function | SmallTest | Level1) +{ + bool originalLockStatus = ScreenManager::GetInstance().IsScreenRotationLocked(); + ScreenManager::GetInstance().SetScreenRotationLocked(!originalLockStatus); + bool modifiedLockedStatus = ScreenManager::GetInstance().IsScreenRotationLocked(); + ScreenManager::GetInstance().SetScreenRotationLocked(originalLockStatus); + ASSERT_EQ(!originalLockStatus, modifiedLockedStatus); +} } } // namespace Rosen } // namespace OHOS diff --git a/dmserver/include/display_manager_interface.h b/dmserver/include/display_manager_interface.h index 862720f7..b6561687 100644 --- a/dmserver/include/display_manager_interface.h +++ b/dmserver/include/display_manager_interface.h @@ -71,6 +71,8 @@ public: TRANS_ID_SCREEN_GET_GAMUT_MAP, TRANS_ID_SCREEN_SET_GAMUT_MAP, TRANS_ID_SCREEN_SET_COLOR_TRANSFORM, + TRANS_ID_IS_SCREEN_ROTATION_LOCKED, + TRANS_ID_SET_SCREEN_ROTATION_LOCKED, }; virtual sptr GetDefaultDisplayInfo() = 0; @@ -83,6 +85,8 @@ public: virtual DMError SetVirtualScreenSurface(ScreenId screenId, sptr surface) = 0; virtual bool SetOrientation(ScreenId screenId, Orientation orientation) = 0; virtual std::shared_ptr GetDisplaySnapshot(DisplayId displayId) = 0; + virtual DMError SetScreenRotationLocked(bool isLocked) = 0; + virtual bool IsScreenRotationLocked() = 0; // colorspace, gamut virtual DMError GetScreenSupportedColorGamuts(ScreenId screenId, std::vector& colorGamuts) = 0; diff --git a/dmserver/include/display_manager_proxy.h b/dmserver/include/display_manager_proxy.h index c0bc28e6..14134a56 100644 --- a/dmserver/include/display_manager_proxy.h +++ b/dmserver/include/display_manager_proxy.h @@ -41,6 +41,8 @@ public: DMError SetVirtualScreenSurface(ScreenId screenId, sptr surface) override; bool SetOrientation(ScreenId screenId, Orientation orientation) override; std::shared_ptr GetDisplaySnapshot(DisplayId displayId) override; + bool IsScreenRotationLocked() override; + DMError SetScreenRotationLocked(bool isLocked) override; // colorspace, gamut DMError GetScreenSupportedColorGamuts(ScreenId screenId, std::vector& colorGamuts) override; diff --git a/dmserver/include/display_manager_service.h b/dmserver/include/display_manager_service.h index ae9ead92..6b0c5e6f 100644 --- a/dmserver/include/display_manager_service.h +++ b/dmserver/include/display_manager_service.h @@ -50,6 +50,8 @@ public: const sptr& displayManagerAgent) override; DMError DestroyVirtualScreen(ScreenId screenId) override; DMError SetVirtualScreenSurface(ScreenId screenId, sptr surface) override; + bool IsScreenRotationLocked() override; + DMError SetScreenRotationLocked(bool isLocked) override; sptr GetDefaultDisplayInfo() override; sptr GetDisplayInfoById(DisplayId displayId) override; diff --git a/dmserver/src/display_manager_proxy.cpp b/dmserver/src/display_manager_proxy.cpp index 63cdfd95..f5608a13 100644 --- a/dmserver/src/display_manager_proxy.cpp +++ b/dmserver/src/display_manager_proxy.cpp @@ -1005,4 +1005,55 @@ bool DisplayManagerProxy::SetVirtualPixelRatio(ScreenId screenId, float virtualP } return reply.ReadBool(); } + +bool DisplayManagerProxy::IsScreenRotationLocked() +{ + bool isLocked; + sptr remote = Remote(); + if (remote == nullptr) { + WLOGFW("IsScreenRotationLocked: remote is nullptr"); + return false; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + WLOGFE("IsScreenRotationLocked: WriteInterfaceToken failed"); + return false; + } + if (remote->SendRequest(static_cast(DisplayManagerMessage::TRANS_ID_IS_SCREEN_ROTATION_LOCKED), + data, reply, option) != ERR_NONE) { + WLOGFW("IsScreenRotationLocked: SendRequest failed"); + return false; + } + isLocked = reply.ReadBool(); + return isLocked; +} + +DMError DisplayManagerProxy::SetScreenRotationLocked(bool isLocked) +{ + sptr remote = Remote(); + if (remote == nullptr) { + WLOGFW("SetScreenRotationLocked: remote is null"); + return DMError::DM_ERROR_REMOTE_CREATE_FAILED; + } + + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + WLOGFE("SetScreenRotationLocked: WriteInterfaceToken failed"); + return DMError::DM_ERROR_WRITE_INTERFACE_TOKEN_FAILED; + } + if (!data.WriteBool(isLocked)) { + WLOGFE("SetScreenRotationLocked: write isLocked failed"); + return DMError::DM_ERROR_IPC_FAILED; + } + if (remote->SendRequest(static_cast(DisplayManagerMessage::TRANS_ID_SET_SCREEN_ROTATION_LOCKED), + data, reply, option) != ERR_NONE) { + WLOGFE("SetScreenRotationLocked: SendRequest failed"); + return DMError::DM_ERROR_IPC_FAILED; + } + return static_cast(reply.ReadInt32()); +} } // namespace OHOS::Rosen \ No newline at end of file diff --git a/dmserver/src/display_manager_service.cpp b/dmserver/src/display_manager_service.cpp index 4d5f67ed..269340f2 100644 --- a/dmserver/src/display_manager_service.cpp +++ b/dmserver/src/display_manager_service.cpp @@ -559,4 +559,15 @@ float DisplayManagerService::GetCustomVirtualPixelRatio() { return DisplayManagerService::customVirtualPixelRatio_; } + +bool DisplayManagerService::IsScreenRotationLocked() +{ + return ScreenRotationController::IsScreenRotationLocked(); +} + +DMError DisplayManagerService::SetScreenRotationLocked(bool isLocked) +{ + ScreenRotationController::SetScreenRotationLocked(isLocked); + return DMError::DM_OK; +} } // namespace OHOS::Rosen \ No newline at end of file diff --git a/dmserver/src/display_manager_stub.cpp b/dmserver/src/display_manager_stub.cpp index 845589a2..ffbce65a 100644 --- a/dmserver/src/display_manager_stub.cpp +++ b/dmserver/src/display_manager_stub.cpp @@ -313,6 +313,17 @@ int32_t DisplayManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, reply.WriteInt32(static_cast(ret)); break; } + case DisplayManagerMessage::TRANS_ID_IS_SCREEN_ROTATION_LOCKED: { + bool isLocked = IsScreenRotationLocked(); + reply.WriteBool(isLocked); + break; + } + case DisplayManagerMessage::TRANS_ID_SET_SCREEN_ROTATION_LOCKED: { + bool isLocked = static_cast(data.ReadBool()); + DMError ret = SetScreenRotationLocked(isLocked); + reply.WriteInt32(static_cast(ret)); + break; + } default: WLOGFW("unknown transaction code"); return IPCObjectStub::OnRemoteRequest(code, data, reply, option); diff --git a/interfaces/innerkits/dm/screen_manager.h b/interfaces/innerkits/dm/screen_manager.h index a2b59dc7..a539049c 100644 --- a/interfaces/innerkits/dm/screen_manager.h +++ b/interfaces/innerkits/dm/screen_manager.h @@ -55,7 +55,8 @@ public: DMError SetVirtualScreenSurface(ScreenId screenId, sptr surface); bool SetScreenPowerForAll(ScreenPowerState state, PowerStateChangeReason reason); ScreenPowerState GetScreenPower(ScreenId screenId); - + DMError SetScreenRotationLocked(bool isLocked); + bool IsScreenRotationLocked(); private: ScreenManager(); ~ScreenManager(); diff --git a/interfaces/kits/napi/screen_runtime/api/@ohos.screen.d.ts b/interfaces/kits/napi/screen_runtime/api/@ohos.screen.d.ts index 8c35b9d5..bf3812dc 100644 --- a/interfaces/kits/napi/screen_runtime/api/@ohos.screen.d.ts +++ b/interfaces/kits/napi/screen_runtime/api/@ohos.screen.d.ts @@ -82,6 +82,21 @@ declare namespace screen { function setVirtualScreenSurface(screenId:number, surfaceId: string, callback: AsyncCallback): void; function setVirtualScreenSurface(screenId:number, surfaceId: string): Promise; + /** + * Get screen rotation lock status. + * @since 9 + */ + function isScreenRotationLocked(callback: AsyncCallback): void; + function isScreenRotationLocked(): Promise; + + /** + * Set screen rotation lock status. + * @param isLocked Indicates whether the screen rotation switch is locked. + * @since 9 + */ + function setScreenRotationLocked(isLocked:boolean, callback: AsyncCallback): void; + function setScreenRotationLocked(isLocked:boolean): Promise; + /** * the parameter of making expand screen * @syscap SystemCapability.WindowManager.WindowManager.Core diff --git a/interfaces/kits/napi/screen_runtime/napi/js_screen_manager.cpp b/interfaces/kits/napi/screen_runtime/napi/js_screen_manager.cpp index af3921b5..31798101 100644 --- a/interfaces/kits/napi/screen_runtime/napi/js_screen_manager.cpp +++ b/interfaces/kits/napi/screen_runtime/napi/js_screen_manager.cpp @@ -98,6 +98,18 @@ static NativeValue* SetVirtualScreenSurface(NativeEngine* engine, NativeCallback JsScreenManager* me = CheckParamsAndGetThis(engine, info); return (me != nullptr) ? me->OnSetVirtualScreenSurface(*engine, *info) : nullptr; } + +static NativeValue* IsScreenRotationLocked(NativeEngine* engine, NativeCallbackInfo* info) +{ + JsScreenManager* me = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnIsScreenRotationLocked(*engine, *info) : nullptr; +} + +static NativeValue* SetScreenRotationLocked(NativeEngine* engine, NativeCallbackInfo* info) +{ + JsScreenManager* me = CheckParamsAndGetThis(engine, info); + return (me != nullptr) ? me->OnSetScreenRotationLocked(*engine, *info) : nullptr; +} private: std::map, sptr>> jsCbMap_; std::mutex mtx_; @@ -607,6 +619,71 @@ NativeValue* OnSetVirtualScreenSurface(NativeEngine& engine, NativeCallbackInfo& engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); return result; } + +NativeValue* OnIsScreenRotationLocked(NativeEngine& engine, NativeCallbackInfo& info) +{ + WLOGFI("OnIsScreenRotationLocked is called"); + DMError errCode = DMError::DM_OK; + if (info.argc > 1) { + WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc); + errCode = DMError::DM_ERROR_INVALID_PARAM; + } + AsyncTask::CompleteCallback complete = + [this](NativeEngine& engine, AsyncTask& task, int32_t status) { + bool isLocked = SingletonContainer::Get().IsScreenRotationLocked(); + task.Resolve(engine, CreateJsValue(engine, isLocked)); + }; + NativeValue* lastParam = nullptr; + if (info.argc == ARGC_ONE && info.argv[ARGC_ONE - 1]->TypeOf() == NATIVE_FUNCTION) { + lastParam = info.argv[ARGC_ONE - 1]; + } + NativeValue* result = nullptr; + AsyncTask::Schedule( + engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); + return result; +} + +NativeValue* OnSetScreenRotationLocked(NativeEngine& engine, NativeCallbackInfo& info) +{ + WLOGFI("JsScreenManager::OnSetScreenRotationLocked is called"); + DMError errCode = DMError::DM_OK; + if (info.argc < 1 || info.argc > 2) { // 2: maximum params num + WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc); + errCode = DMError::DM_ERROR_INVALID_PARAM; + } + bool isLocked = false; + if (errCode == DMError::DM_OK) { + NativeBoolean* nativeVal = ConvertNativeValueTo(info.argv[0]); + if (nativeVal == nullptr) { + WLOGFE("[NAPI]Failed to convert parameter to isLocked"); + errCode = DMError::DM_ERROR_INVALID_PARAM; + } else { + isLocked = static_cast(*nativeVal); + } + } + + AsyncTask::CompleteCallback complete = + [isLocked, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) { + if (errCode != DMError::DM_OK) { + task.Reject(engine, CreateJsError(engine, static_cast(errCode), "Invalidate params.")); + return; + } + DMError ret = SingletonContainer::Get().SetScreenRotationLocked(isLocked); + if (ret == DMError::DM_OK) { + task.Resolve(engine, engine.CreateUndefined()); + } else { + task.Reject(engine, CreateJsError(engine, static_cast(ret), + "JsScreenManager:SetScreenRotationLocked failed.")); + } + WLOGFI("JsScreenManager::OnSetScreenRotationLocked success, ret = %{public}d", ret); + }; + NativeValue* lastParam = (info.argc <= 1) ? nullptr : + (info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr); + NativeValue* result = nullptr; + AsyncTask::Schedule( + engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); + return result; +} }; NativeValue* JsScreenManagerInit(NativeEngine* engine, NativeValue* exportObj) @@ -635,6 +712,8 @@ NativeValue* JsScreenManagerInit(NativeEngine* engine, NativeValue* exportObj) BindNativeFunction(*engine, *object, "createVirtualScreen", JsScreenManager::CreateVirtualScreen); BindNativeFunction(*engine, *object, "destroyVirtualScreen", JsScreenManager::DestroyVirtualScreen); BindNativeFunction(*engine, *object, "setVirtualScreenSurface", JsScreenManager::SetVirtualScreenSurface); + BindNativeFunction(*engine, *object, "setScreenRotationLocked", JsScreenManager::SetScreenRotationLocked); + BindNativeFunction(*engine, *object, "isScreenRotationLocked", JsScreenManager::IsScreenRotationLocked); return engine->CreateUndefined(); } } // namespace Rosen