diff --git a/frameworks/js/napi/audiomanager/napi_audio_volume_manager.cpp b/frameworks/js/napi/audiomanager/napi_audio_volume_manager.cpp index f564ff131f9a1f50f8e60e281c989292759b6cfd..e022451a6f28dce9d11d85cf492dea2fcc3d987e 100644 --- a/frameworks/js/napi/audiomanager/napi_audio_volume_manager.cpp +++ b/frameworks/js/napi/audiomanager/napi_audio_volume_manager.cpp @@ -172,6 +172,8 @@ napi_value NapiAudioVolumeManager::Init(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getSupportedAudioVolumeTypes", GetSupportedAudioVolumeTypes), DECLARE_NAPI_FUNCTION("getAudioVolumeTypeByStreamUsage", GetAudioVolumeTypeByStreamUsage), DECLARE_NAPI_FUNCTION("getStreamUsagesByVolumeType", GetStreamUsagesByVolumeType), + DECLARE_NAPI_FUNCTION("getSystemVolumeByUid", GetSystemVolumeByUid), + DECLARE_NAPI_FUNCTION("setSystemVolumeByUid", SetSystemVolumeByUid), DECLARE_NAPI_FUNCTION("on", On), DECLARE_NAPI_FUNCTION("off", Off), DECLARE_NAPI_FUNCTION("forceVolumeKeyControlType", ForceVolumeKeyControlType), @@ -438,6 +440,91 @@ napi_value NapiAudioVolumeManager::IsAppVolumeMutedForUid(napi_env env, napi_cal return NapiAsyncWork::Enqueue(env, context, "IsAppVolumeMutedForUid", executor, complete); } +napi_value NapiAudioVolumeManager::GetSystemVolumeByUid(napi_env env, napi_callback_info info) +{ + napi_value result = nullptr; + size_t argc = ARGS_TWO; + napi_value args[ARGS_TWO] = {}; + auto *napiAudioVolumeManager = GetParamWithSync(env, info, argc, args); + CHECK_AND_RETURN_RET_LOG(argc == ARGS_TWO, NapiAudioError::ThrowErrorAndReturn(env, + NAPI_ERR_INPUT_INVALID, "mandatory parameters are left unspecified"), "invalid arguments"); + + for (size_t i = PARAM0; i < argc; i++) { + napi_valuetype valueType = napi_undefined; + napi_typeof(env, args[i], &valueType); + CHECK_AND_RETURN_RET_LOG(valueType == napi_number, NapiAudioError::ThrowErrorAndReturn(env, + NAPI_ERR_INPUT_INVALID, "incorrect parameter types: The type of parameter must be number"), + "invalid valueType"); + } + int32_t volType; + NapiParamUtils::GetValueInt32(env, volType, args[PARAM0]); + CHECK_AND_RETURN_RET_LOG(NapiAudioEnum::IsLegalInputArgumentVolType(volType), + NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM, + "parameter verification failed: The param of volumeType must be enum AudioVolumeType"), "get volType failed"); + + int32_t uid; + NapiParamUtils::GetValueInt32(env, uid, args[PARAM1]); + CHECK_AND_RETURN_RET_LOG(uid >= 0, + NapiAudioError::ThrowErrorAndReturn(env, NAPI_ERR_INVALID_PARAM, + "parameter verification failed: The param of volumeType must be greater than zero"), "get uid failed"); + + CHECK_AND_RETURN_RET_LOG(napiAudioVolumeManager != nullptr, result, "napiAudioVolumeManager is nullptr"); + CHECK_AND_RETURN_RET_LOG(napiAudioVolumeManager->audioSystemMngr_ != nullptr, result, + "audioSystemMngr_ is nullptr"); + int32_t volLevel = napiAudioVolumeManager->audioSystemMngr_->GetVolume( + NapiAudioEnum::GetNativeAudioVolumeType(volType), uid); + NapiParamUtils::SetValueInt32(env, volLevel, result); + + return result; +} + +napi_value NapiAudioVolumeManager::SetSystemVolumeByUid(napi_env env, napi_callback_info info) +{ + auto context = std::make_shared(); + if (context == nullptr) { + AUDIO_ERR_LOG("SetSystemVolumeByUid failed : no memory"); + NapiAudioError::ThrowError(env, "SetSystemVolumeByUid failed : no memory", NAPI_ERR_NO_MEMORY); + return NapiParamUtils::GetUndefinedValue(env); + } + + auto inputParser = [env, context](size_t argc, napi_value *argv) { + NAPI_CHECK_ARGS_RETURN_VOID(context, argc >= ARGS_THREE, "invalid arguments", + NAPI_ERR_INVALID_PARAM); + context->status = NapiParamUtils::GetValueInt32(env, context->volumeType, argv[PARAM0]); + NAPI_CHECK_ARGS_RETURN_VOID(context, context->status == napi_ok, "get volumeType failed", + NAPI_ERR_INVALID_PARAM); + if (!NapiAudioEnum::IsLegalInputArgumentVolType(context->volumeType)) { + context->SignError(context->errCode == NAPI_ERR_INVALID_PARAM? + NAPI_ERR_INVALID_PARAM : NAPI_ERR_UNSUPPORTED); + } + context->status = NapiParamUtils::GetValueInt32(env, context->volLevel, argv[PARAM1]); + NAPI_CHECK_ARGS_RETURN_VOID(context, context->status == napi_ok, "get volLevel failed", + NAPI_ERR_INVALID_PARAM); + context->status = NapiParamUtils::GetValueInt32(env, context->appUid, argv[PARAM2]); + NAPI_CHECK_ARGS_RETURN_VOID(context, context->status == napi_ok, "get appUid failed", + NAPI_ERR_INVALID_PARAM); + }; + context->GetCbInfo(env, info, inputParser); + + auto executor = [context]() { + CHECK_AND_RETURN_LOG(CheckContextStatus(context), "context object state is error."); + auto obj = reinterpret_cast(context->native); + ObjectRefMap objectGuard(obj); + auto *napiAudioVolumeManager = objectGuard.GetPtr(); + CHECK_AND_RETURN_LOG(CheckAudioVolumeManagerStatus(napiAudioVolumeManager, context), + "audio volume manager state is error."); + context->intValue = napiAudioVolumeManager->audioSystemMngr_->SetVolume( + NapiAudioEnum::GetNativeAudioVolumeType(context->volumeType), context->volLevel, context->appUid); + NAPI_CHECK_ARGS_RETURN_VOID(context, context->intValue == SUCCESS, "SetSystemVolumeByUid failed", + NAPI_ERR_SYSTEM); + }; + + auto complete = [env](napi_value &output) { + output = NapiParamUtils::GetUndefinedValue(env); + }; + return NapiAsyncWork::Enqueue(env, context, "SetSystemVolumeByUid", executor, complete); +} + napi_value NapiAudioVolumeManager::GetVolumeGroupInfos(napi_env env, napi_callback_info info) { auto context = std::make_shared(); diff --git a/frameworks/js/napi/audiomanager/napi_audio_volume_manager.h b/frameworks/js/napi/audiomanager/napi_audio_volume_manager.h index f1149654e88a6f2388880a28d3892731503b9e8d..0dd7a5f6a543c3761c09c55f50f44e1e703b45e6 100644 --- a/frameworks/js/napi/audiomanager/napi_audio_volume_manager.h +++ b/frameworks/js/napi/audiomanager/napi_audio_volume_manager.h @@ -84,6 +84,8 @@ private: static napi_value GetSupportedAudioVolumeTypes(napi_env env, napi_callback_info info); static napi_value GetAudioVolumeTypeByStreamUsage(napi_env env, napi_callback_info info); static napi_value GetStreamUsagesByVolumeType(napi_env env, napi_callback_info info); + static napi_value GetSystemVolumeByUid(napi_env env, napi_callback_info info); + static napi_value SetSystemVolumeByUid(napi_env env, napi_callback_info info); static napi_value On(napi_env env, napi_callback_info info); static napi_value RegisterCallback(napi_env env, napi_value jsThis, size_t argc, napi_value *args, const std::string &cbName); diff --git a/interfaces/inner_api/native/audiomanager/include/audio_system_manager.h b/interfaces/inner_api/native/audiomanager/include/audio_system_manager.h index 344265be60d6a60e219a69a6bd33e19cf65c8f2e..8c3f9d5b6721c508618d1d142246e1f7f0ff608c 100644 --- a/interfaces/inner_api/native/audiomanager/include/audio_system_manager.h +++ b/interfaces/inner_api/native/audiomanager/include/audio_system_manager.h @@ -66,7 +66,7 @@ public: * defined in {@link audio_errors.h} otherwise. * @since 8 */ - int32_t SetVolume(AudioVolumeType volumeType, int32_t volume) const; + int32_t SetVolume(AudioVolumeType volumeType, int32_t volume, int32_t uid = 0) const; /** * @brief Set the stream volume. @@ -214,7 +214,7 @@ public: * @return Returns current stream volume. * @since 8 */ - int32_t GetVolume(AudioVolumeType volumeType) const; + int32_t GetVolume(AudioVolumeType volumeType, int32_t uid = 0) const; /** * @brief Set volume discount factor. diff --git a/services/audio_service/client/src/audio_system_manager.cpp b/services/audio_service/client/src/audio_system_manager.cpp index 05f3c2f9043151fe1eea648070fc1843735bd51e..9c035e0085886079bc86919e3ef4ef022de15902 100644 --- a/services/audio_service/client/src/audio_system_manager.cpp +++ b/services/audio_service/client/src/audio_system_manager.cpp @@ -629,7 +629,7 @@ int32_t AudioSystemManager::SetActiveVolumeTypeCallback( return AudioPolicyManager::GetInstance().SetActiveVolumeTypeCallback(callback); } -int32_t AudioSystemManager::SetVolume(AudioVolumeType volumeType, int32_t volumeLevel) const +int32_t AudioSystemManager::SetVolume(AudioVolumeType volumeType, int32_t volumeLevel, int32_t uid) const { AUDIO_INFO_LOG("SetSystemVolume: volumeType[%{public}d], volumeLevel[%{public}d]", volumeType, volumeLevel); @@ -657,7 +657,7 @@ int32_t AudioSystemManager::SetVolume(AudioVolumeType volumeType, int32_t volume } /* Call Audio Policy SetSystemVolumeLevel */ - return AudioPolicyManager::GetInstance().SetSystemVolumeLevel(volumeType, volumeLevel, true); + return AudioPolicyManager::GetInstance().SetSystemVolumeLevel(volumeType, volumeLevel, uid == 0, 0, uid); } int32_t AudioSystemManager::SetVolumeWithDevice(AudioVolumeType volumeType, int32_t volumeLevel, @@ -693,7 +693,7 @@ int32_t AudioSystemManager::SetVolumeWithDevice(AudioVolumeType volumeType, int3 return AudioPolicyManager::GetInstance().SetSystemVolumeLevelWithDevice(volumeType, volumeLevel, deviceType); } -int32_t AudioSystemManager::GetVolume(AudioVolumeType volumeType) const +int32_t AudioSystemManager::GetVolume(AudioVolumeType volumeType, int32_t uid) const { switch (volumeType) { case STREAM_MUSIC: @@ -718,7 +718,7 @@ int32_t AudioSystemManager::GetVolume(AudioVolumeType volumeType) const return ERR_NOT_SUPPORTED; } - return AudioPolicyManager::GetInstance().GetSystemVolumeLevel(volumeType); + return AudioPolicyManager::GetInstance().GetSystemVolumeLevel(volumeType, uid); } int32_t AudioSystemManager::SetLowPowerVolume(int32_t streamId, float volume) const