diff --git a/frameworks/js/napi/vibrator/include/vibrator_napi_utils.h b/frameworks/js/napi/vibrator/include/vibrator_napi_utils.h index 6350f6887a29408968da874244382b072496a621..87a6191a2f89d2bd032a66ce99359d8e6521f81e 100644 --- a/frameworks/js/napi/vibrator/include/vibrator_napi_utils.h +++ b/frameworks/js/napi/vibrator/include/vibrator_napi_utils.h @@ -70,6 +70,7 @@ bool GetInt32Value(const napi_env &env, const napi_value &value, int32_t &result bool GetDoubleValue(const napi_env &env, const napi_value &value, double &result); bool GetInt64Value(const napi_env &env, const napi_value &value, int64_t &result); bool GetStringValue(const napi_env &env, const napi_value &value, string &result); +bool GetPropertyBool(const napi_env &env, const napi_value &value, const std::string &type, bool &result); bool GetPropertyItem(const napi_env &env, const napi_value &value, const std::string &type, napi_value &item); bool GetPropertyString(const napi_env &env, const napi_value &value, const std::string &type, std::string &result); bool GetPropertyInt32(const napi_env &env, const napi_value &value, const std::string &type, int32_t &result); diff --git a/frameworks/js/napi/vibrator/src/vibrator_js.cpp b/frameworks/js/napi/vibrator/src/vibrator_js.cpp index a9e42d19fc16ef55ab96ff3225d1016cea05ba5f..1eb03af0c874ca64cf405079bebcc2fb01af6a44 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_js.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_js.cpp @@ -73,6 +73,7 @@ static std::set g_allowedTypes = {"time", "preset", "file", "patter struct VibrateInfo { std::string type; std::string usage; + bool systemUsage; int32_t duration = 0; std::string effectId; int32_t count = 0; @@ -447,22 +448,25 @@ bool ParseParameter(napi_env env, napi_value args[], size_t argc, VibrateInfo &i CHKCF(CheckVibratorPatternParameter(info.vibratorPattern), "CheckVibratorPatternParameter fail"); } CHKCF(GetPropertyString(env, args[1], "usage", info.usage), "Get vibrate usage fail"); + if (!GetPropertyBool(env, args[1], "systemUsage", info.systemUsage)) { + info.systemUsage = false; + } return true; } -bool SetUsage(const std::string &usage) +bool SetUsage(const std::string &usage, bool systemUsage) { if (auto iter = g_usageType.find(usage); iter == g_usageType.end()) { MISC_HILOGE("Wrong usage type"); return false; } - return SetUsage(g_usageType[usage]); + return SetUsage(g_usageType[usage], systemUsage); } int32_t StartVibrate(const VibrateInfo &info) { CALL_LOG_ENTER; - if (!SetUsage(info.usage)) { + if (!SetUsage(info.usage, info.systemUsage)) { MISC_HILOGE("SetUsage fail"); return PARAMETER_ERROR; } diff --git a/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp b/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp index 654b4abb96de53c6ccd147a75f2845c45e678408..873802d262b92af4899de90e8c349b16da3640ce 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp @@ -117,6 +117,16 @@ bool GetInt64Value(const napi_env &env, const napi_value &value, int64_t &result return true; } +bool GetBoolValue(const napi_env &env, const napi_value &value, bool &result) +{ + CALL_LOG_ENTER; + napi_valuetype valuetype = napi_undefined; + CHKCF(napi_typeof(env, value, &valuetype) == napi_ok, "napi_typeof failed"); + CHKCF((valuetype == napi_boolean), "Wrong argument type. bool expected"); + CHKCF(napi_get_value_bool(env, value, &result) == napi_ok, "napi_get_value_bool failed"); + return true; +} + bool GetStringValue(const napi_env &env, const napi_value &value, string &result) { CALL_LOG_ENTER; @@ -208,6 +218,23 @@ bool GetPropertyInt64(const napi_env &env, const napi_value &value, const std::s return true; } +bool GetPropertyBool(const napi_env &env, const napi_value &value, const std::string &type, bool &result) +{ + bool exist = false; + napi_status status = napi_has_named_property(env, value, type.c_str(), &exist); + if ((status != napi_ok) || (!exist)) { + MISC_HILOGD("Can not find %{public}s property", type.c_str()); + return false; + } + napi_value item = nullptr; + CHKCF((napi_get_named_property(env, value, type.c_str(), &item) == napi_ok), "napi get property fail"); + if (!GetBoolValue(env, item, result)) { + MISC_HILOGE("Get bool value fail"); + return false; + } + return true; +} + std::map g_convertFuncList = { {COMMON_CALLBACK, ConstructCommonResult}, {IS_SUPPORT_EFFECT_CALLBACK, ConstructIsSupportEffectResult}, diff --git a/frameworks/native/common/include/i_miscdevice_service.h b/frameworks/native/common/include/i_miscdevice_service.h index 2c69c91936c89746bc6d4ea3cb3185727567722b..f555c7a243b414b5b83f3b6b03e7410d17b52198 100644 --- a/frameworks/native/common/include/i_miscdevice_service.h +++ b/frameworks/native/common/include/i_miscdevice_service.h @@ -36,12 +36,12 @@ public: IMiscdeviceService() = default; virtual ~IMiscdeviceService() = default; DECLARE_INTERFACE_DESCRIPTOR(u"IMiscdeviceService"); - virtual int32_t Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage) = 0; + virtual int32_t Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage, bool systemUsage) = 0; virtual int32_t PlayVibratorEffect(int32_t vibratorId, const std::string &effect, - int32_t loopCount, int32_t usage) = 0; + int32_t loopCount, int32_t usage, bool systemUsage) = 0; #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM virtual int32_t PlayVibratorCustom(int32_t vibratorId, const RawFileDescriptor &rawFd, int32_t usage, - const VibrateParameter ¶meter) = 0; + bool systemUsage, const VibrateParameter ¶meter) = 0; #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM virtual int32_t StopVibrator(int32_t vibratorId) = 0; virtual int32_t StopVibrator(int32_t vibratorId, const std::string &mode) = 0; @@ -50,10 +50,11 @@ public: virtual int32_t TurnOn(int32_t lightId, const LightColor &color, const LightAnimationIPC &animation) = 0; virtual int32_t TurnOff(int32_t lightId) = 0; virtual int32_t GetDelayTime(int32_t &delayTime) = 0; - virtual int32_t PlayPattern(const VibratePattern &pattern, int32_t usage, const VibrateParameter ¶meter) = 0; + virtual int32_t PlayPattern(const VibratePattern &pattern, int32_t usage, bool systemUsage, + const VibrateParameter ¶meter) = 0; virtual int32_t TransferClientRemoteObject(const sptr &vibratorClient) = 0; virtual int32_t PlayPrimitiveEffect(int32_t vibratorId, const std::string &effect, int32_t intensity, - int32_t usage, int32_t count) = 0; + int32_t usage, bool systemUsage, int32_t count) = 0; virtual int32_t GetVibratorCapacity(VibratorCapacity &capacity) = 0; }; } // namespace Sensors diff --git a/frameworks/native/common/include/miscdevice_service_proxy.h b/frameworks/native/common/include/miscdevice_service_proxy.h index 370e0a93fdedd2108f956377f2e77bcd97400885..9af91e7ba1a45e206121e0a8f811a027f6eeb2b7 100644 --- a/frameworks/native/common/include/miscdevice_service_proxy.h +++ b/frameworks/native/common/include/miscdevice_service_proxy.h @@ -27,12 +27,12 @@ class MiscdeviceServiceProxy : public IRemoteProxy { public: explicit MiscdeviceServiceProxy(const sptr &impl); ~MiscdeviceServiceProxy() = default; - virtual int32_t Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage) override; + virtual int32_t Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage, bool systemUsage) override; virtual int32_t PlayVibratorEffect(int32_t vibratorId, const std::string &effect, - int32_t loopCount, int32_t usage) override; + int32_t loopCount, int32_t usage, bool systemUsage) override; #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM virtual int32_t PlayVibratorCustom(int32_t vibratorId, const RawFileDescriptor &rawFd, int32_t usage, - const VibrateParameter ¶meter) override; + bool systemUsage, const VibrateParameter ¶meter) override; #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM virtual int32_t StopVibrator(int32_t vibratorId) override; virtual int32_t StopVibrator(int32_t vibratorId, const std::string &mode) override; @@ -42,10 +42,10 @@ public: virtual int32_t TurnOff(int32_t lightId) override; virtual int32_t GetDelayTime(int32_t &delayTime) override; virtual int32_t PlayPattern(const VibratePattern &pattern, int32_t usage, - const VibrateParameter ¶meter) override; + bool systemUsage, const VibrateParameter ¶meter) override; virtual int32_t TransferClientRemoteObject(const sptr &vibratorClient) override; virtual int32_t PlayPrimitiveEffect(int32_t vibratorId, const std::string &effect, int32_t intensity, - int32_t usage, int32_t count) override; + int32_t usage, bool systemUsage, int32_t count) override; virtual int32_t GetVibratorCapacity(VibratorCapacity &capacity) override; private: diff --git a/frameworks/native/common/src/miscdevice_service_proxy.cpp b/frameworks/native/common/src/miscdevice_service_proxy.cpp index 7e00a34207e357d67ff730b94666f63ba2535d20..9afc992771b396960207830762ab9a1414c6252f 100644 --- a/frameworks/native/common/src/miscdevice_service_proxy.cpp +++ b/frameworks/native/common/src/miscdevice_service_proxy.cpp @@ -34,7 +34,7 @@ constexpr int32_t MAX_LIGHT_COUNT = 0XFF; MiscdeviceServiceProxy::MiscdeviceServiceProxy(const sptr &impl) : IRemoteProxy(impl) {} -int32_t MiscdeviceServiceProxy::Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage) +int32_t MiscdeviceServiceProxy::Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage, bool systemUsage) { MessageParcel data; if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { @@ -53,6 +53,10 @@ int32_t MiscdeviceServiceProxy::Vibrate(int32_t vibratorId, int32_t timeOut, int MISC_HILOGE("WriteUint32 usage failed"); return WRITE_MSG_ERR; } + if (!data.WriteBool(systemUsage)) { + MISC_HILOGE("WritBool systemUsage failed"); + return WRITE_MSG_ERR; + } sptr remote = Remote(); CHKPR(remote, ERROR); MessageParcel reply; @@ -93,7 +97,7 @@ int32_t MiscdeviceServiceProxy::StopVibrator(int32_t vibratorId) } int32_t MiscdeviceServiceProxy::PlayVibratorEffect(int32_t vibratorId, const std::string &effect, - int32_t loopCount, int32_t usage) + int32_t loopCount, int32_t usage, bool systemUsage) { MessageParcel data; if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { @@ -116,6 +120,10 @@ int32_t MiscdeviceServiceProxy::PlayVibratorEffect(int32_t vibratorId, const std MISC_HILOGE("Writeint32 usage failed"); return WRITE_MSG_ERR; } + if (!data.WriteBool(systemUsage)) { + MISC_HILOGE("Writebool systemUsage failed"); + return WRITE_MSG_ERR; + } sptr remote = Remote(); CHKPR(remote, ERROR); MessageParcel reply; @@ -191,7 +199,7 @@ int32_t MiscdeviceServiceProxy::IsSupportEffect(const std::string &effect, bool #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM int32_t MiscdeviceServiceProxy::PlayVibratorCustom(int32_t vibratorId, const RawFileDescriptor &rawFd, int32_t usage, - const VibrateParameter ¶meter) + bool systemUsage, const VibrateParameter ¶meter) { MessageParcel data; if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { @@ -206,6 +214,10 @@ int32_t MiscdeviceServiceProxy::PlayVibratorCustom(int32_t vibratorId, const Raw MISC_HILOGE("Writeint32 usage failed"); return WRITE_MSG_ERR; } + if (!data.WriteBool(systemUsage)) { + MISC_HILOGE("Writebool systemUsage failed"); + return WRITE_MSG_ERR; + } if (!parameter.Marshalling(data)) { MISC_HILOGE("Write adjust parameter failed"); return WRITE_MSG_ERR; @@ -359,7 +371,7 @@ int32_t MiscdeviceServiceProxy::GetDelayTime(int32_t &delayTime) } int32_t MiscdeviceServiceProxy::PlayPattern(const VibratePattern &pattern, int32_t usage, - const VibrateParameter ¶meter) + bool systemUsage, const VibrateParameter ¶meter) { MessageParcel data; if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) { @@ -374,6 +386,10 @@ int32_t MiscdeviceServiceProxy::PlayPattern(const VibratePattern &pattern, int32 MISC_HILOGE("WriteUint32 usage failed"); return WRITE_MSG_ERR; } + if (!data.WriteBool(systemUsage)) { + MISC_HILOGE("WriteBool systemUsage failed"); + return WRITE_MSG_ERR; + } if (!parameter.Marshalling(data)) { MISC_HILOGE("Write adjust parameter failed"); return WRITE_MSG_ERR; @@ -418,7 +434,7 @@ int32_t MiscdeviceServiceProxy::TransferClientRemoteObject(const sptr { public: ~VibratorServiceClient() override; - int32_t Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage); - int32_t Vibrate(int32_t vibratorId, const std::string &effect, int32_t loopCount, int32_t usage); + int32_t Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage, bool systemUsage); + int32_t Vibrate(int32_t vibratorId, const std::string &effect, int32_t loopCount, int32_t usage, bool systemUsage); #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM int32_t PlayVibratorCustom(int32_t vibratorId, const RawFileDescriptor &rawFd, int32_t usage, - const VibratorParameter ¶meter); + bool systemUsage, const VibratorParameter ¶meter); #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM int32_t StopVibrator(int32_t vibratorId, const std::string &mode); int32_t StopVibrator(int32_t vibratorId); @@ -67,10 +67,11 @@ public: void ProcessDeathObserver(const wptr &object); int32_t PreProcess(const VibratorFileDescription &fd, VibratorPackage &package); int32_t GetDelayTime(int32_t &delayTime); - int32_t PlayPattern(const VibratorPattern &pattern, int32_t usage, const VibratorParameter ¶meter); + int32_t PlayPattern(const VibratorPattern &pattern, int32_t usage, bool systemUsage, + const VibratorParameter ¶meter); int32_t FreeVibratorPackage(VibratorPackage &package); int32_t PlayPrimitiveEffect(int32_t vibratorId, const std::string &effect, int32_t intensity, int32_t usage, - int32_t count); + bool systemUsage, int32_t count); bool IsSupportVibratorCustom(); private: diff --git a/frameworks/native/vibrator/src/vibrator_service_client.cpp b/frameworks/native/vibrator/src/vibrator_service_client.cpp index 93bbad2f554fe01426c3677d0bb25536fd6c0486..dc0e8d7499563f08e7d256175932f303422c62a5 100644 --- a/frameworks/native/vibrator/src/vibrator_service_client.cpp +++ b/frameworks/native/vibrator/src/vibrator_service_client.cpp @@ -120,7 +120,7 @@ int32_t VibratorServiceClient::TransferClientRemoteObject() return ret; } -int32_t VibratorServiceClient::Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage) +int32_t VibratorServiceClient::Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage, bool systemUsage) { MISC_HILOGD("Vibrate begin, time:%{public}d, usage:%{public}d", timeOut, usage); int32_t ret = InitServiceClient(); @@ -130,7 +130,7 @@ int32_t VibratorServiceClient::Vibrate(int32_t vibratorId, int32_t timeOut, int3 } CHKPR(miscdeviceProxy_, ERROR); StartTrace(HITRACE_TAG_SENSORS, "VibrateTime"); - ret = miscdeviceProxy_->Vibrate(vibratorId, timeOut, usage); + ret = miscdeviceProxy_->Vibrate(vibratorId, timeOut, usage, systemUsage); FinishTrace(HITRACE_TAG_SENSORS); if (ret != ERR_OK) { MISC_HILOGE("Vibrate time failed, ret:%{public}d, time:%{public}d, usage:%{public}d", ret, timeOut, usage); @@ -139,7 +139,7 @@ int32_t VibratorServiceClient::Vibrate(int32_t vibratorId, int32_t timeOut, int3 } int32_t VibratorServiceClient::Vibrate(int32_t vibratorId, const std::string &effect, - int32_t loopCount, int32_t usage) + int32_t loopCount, int32_t usage, bool systemUsage) { MISC_HILOGD("Vibrate begin, effect:%{public}s, loopCount:%{public}d, usage:%{public}d", effect.c_str(), loopCount, usage); @@ -150,7 +150,7 @@ int32_t VibratorServiceClient::Vibrate(int32_t vibratorId, const std::string &ef } CHKPR(miscdeviceProxy_, ERROR); StartTrace(HITRACE_TAG_SENSORS, "VibrateEffect"); - ret = miscdeviceProxy_->PlayVibratorEffect(vibratorId, effect, loopCount, usage); + ret = miscdeviceProxy_->PlayVibratorEffect(vibratorId, effect, loopCount, usage, systemUsage); FinishTrace(HITRACE_TAG_SENSORS); if (ret != ERR_OK) { MISC_HILOGE("Vibrate effect failed, ret:%{public}d, effect:%{public}s, loopCount:%{public}d, usage:%{public}d", @@ -161,7 +161,7 @@ int32_t VibratorServiceClient::Vibrate(int32_t vibratorId, const std::string &ef #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM int32_t VibratorServiceClient::PlayVibratorCustom(int32_t vibratorId, const RawFileDescriptor &rawFd, int32_t usage, - const VibratorParameter ¶meter) + bool systemUsage, const VibratorParameter ¶meter) { MISC_HILOGD("Vibrate begin, fd:%{public}d, offset:%{public}lld, length:%{public}lld, usage:%{public}d", rawFd.fd, static_cast(rawFd.offset), static_cast(rawFd.length), usage); @@ -176,7 +176,7 @@ int32_t VibratorServiceClient::PlayVibratorCustom(int32_t vibratorId, const RawF .intensity = parameter.intensity, .frequency = parameter.frequency }; - ret = miscdeviceProxy_->PlayVibratorCustom(vibratorId, rawFd, usage, vibateParameter); + ret = miscdeviceProxy_->PlayVibratorCustom(vibratorId, rawFd, usage, systemUsage, vibateParameter); FinishTrace(HITRACE_TAG_SENSORS); if (ret != ERR_OK) { MISC_HILOGE("PlayVibratorCustom failed, ret:%{public}d, usage:%{public}d", ret, usage); @@ -335,7 +335,7 @@ int32_t VibratorServiceClient::GetDelayTime(int32_t &delayTime) } int32_t VibratorServiceClient::PlayPattern(const VibratorPattern &pattern, int32_t usage, - const VibratorParameter ¶meter) + bool systemUsage, const VibratorParameter ¶meter) { MISC_HILOGD("Vibrate begin, usage:%{public}d", usage); int32_t ret = InitServiceClient(); @@ -377,7 +377,7 @@ int32_t VibratorServiceClient::PlayPattern(const VibratorPattern &pattern, int32 .intensity = parameter.intensity, .frequency = parameter.frequency }; - ret = miscdeviceProxy_->PlayPattern(vibratePattern, usage, vibateParameter); + ret = miscdeviceProxy_->PlayPattern(vibratePattern, usage, systemUsage, vibateParameter); FinishTrace(HITRACE_TAG_SENSORS); if (ret != ERR_OK) { MISC_HILOGE("PlayPattern failed, ret:%{public}d, usage:%{public}d", ret, usage); @@ -468,7 +468,7 @@ int32_t VibratorServiceClient::FreeVibratorPackage(VibratorPackage &package) } int32_t VibratorServiceClient::PlayPrimitiveEffect(int32_t vibratorId, const std::string &effect, int32_t intensity, - int32_t usage, int32_t count) + int32_t usage, bool systemUsage, int32_t count) { MISC_HILOGD("Vibrate begin, effect:%{public}s, intensity:%{public}d, usage:%{public}d, count:%{public}d", effect.c_str(), intensity, usage, count); @@ -479,7 +479,7 @@ int32_t VibratorServiceClient::PlayPrimitiveEffect(int32_t vibratorId, const std } CHKPR(miscdeviceProxy_, ERROR); StartTrace(HITRACE_TAG_SENSORS, "PlayPrimitiveEffect"); - ret = miscdeviceProxy_->PlayPrimitiveEffect(vibratorId, effect, intensity, usage, count); + ret = miscdeviceProxy_->PlayPrimitiveEffect(vibratorId, effect, intensity, usage, systemUsage, count); FinishTrace(HITRACE_TAG_SENSORS); if (ret != ERR_OK) { MISC_HILOGE("Play primitive effect failed, ret:%{public}d, effect:%{public}s, intensity:%{public}d," diff --git a/frameworks/native/vibrator/vibrator_agent.cpp b/frameworks/native/vibrator/vibrator_agent.cpp index 45fcf99b7be13d1fe59bdc1acebe1168d85382a3..8f0f17f868f662fe7790a3002dd71b3fd607e9a1 100644 --- a/frameworks/native/vibrator/vibrator_agent.cpp +++ b/frameworks/native/vibrator/vibrator_agent.cpp @@ -30,6 +30,7 @@ namespace { constexpr int32_t DEFAULT_VIBRATOR_ID = 123; int32_t g_loopCount = 1; int32_t g_usage = USAGE_UNKNOWN; +bool g_systemUsage = false; VibratorParameter g_vibratorParameter; const std::string PHONE_TYPE = "phone"; const int32_t INTENSITY_ADJUST_MIN = 0; @@ -71,9 +72,10 @@ int32_t StartVibrator(const char *effectId) MISC_HILOGD("Time delay measurement:start time"); CHKPR(effectId, PARAMETER_ERROR); auto &client = VibratorServiceClient::GetInstance(); - int32_t ret = client.Vibrate(DEFAULT_VIBRATOR_ID, effectId, g_loopCount, g_usage); + int32_t ret = client.Vibrate(DEFAULT_VIBRATOR_ID, effectId, g_loopCount, g_usage, g_systemUsage); g_loopCount = 1; g_usage = USAGE_UNKNOWN; + g_systemUsage = false; if (ret != ERR_OK) { MISC_HILOGD("Vibrate effectId failed, ret:%{public}d", ret); return NormalizeErrCode(ret); @@ -88,8 +90,9 @@ int32_t StartVibratorOnce(int32_t duration) return PARAMETER_ERROR; } auto &client = VibratorServiceClient::GetInstance(); - int32_t ret = client.Vibrate(DEFAULT_VIBRATOR_ID, duration, g_usage); + int32_t ret = client.Vibrate(DEFAULT_VIBRATOR_ID, duration, g_usage, g_systemUsage); g_usage = USAGE_UNKNOWN; + g_systemUsage = false; if (ret != ERR_OK) { MISC_HILOGD("Vibrate duration failed, ret:%{public}d", ret); return NormalizeErrCode(ret); @@ -118,8 +121,9 @@ int32_t PlayVibratorCustom(int32_t fd, int64_t offset, int64_t length) .offset = offset, .length = length }; - int32_t ret = client.PlayVibratorCustom(DEFAULT_VIBRATOR_ID, rawFd, g_usage, g_vibratorParameter); + int32_t ret = client.PlayVibratorCustom(DEFAULT_VIBRATOR_ID, rawFd, g_usage, g_systemUsage, g_vibratorParameter); g_usage = USAGE_UNKNOWN; + g_systemUsage = false; g_vibratorParameter.intensity = INTENSITY_ADJUST_MAX; g_vibratorParameter.frequency = 0; if (ret != ERR_OK) { @@ -160,13 +164,14 @@ int32_t Cancel() return SUCCESS; } -bool SetUsage(int32_t usage) +bool SetUsage(int32_t usage, bool systemUsage) { if ((usage < 0) || (usage >= USAGE_MAX)) { MISC_HILOGE("Input invalid, usage is %{public}d", usage); return false; } g_usage = usage; + g_systemUsage = systemUsage; return true; } @@ -213,8 +218,9 @@ int32_t GetDelayTime(int32_t &delayTime) int32_t PlayPattern(const VibratorPattern &pattern) { auto &client = VibratorServiceClient::GetInstance(); - int32_t ret = client.PlayPattern(pattern, g_usage, g_vibratorParameter); + int32_t ret = client.PlayPattern(pattern, g_usage, g_systemUsage, g_vibratorParameter); g_usage = USAGE_UNKNOWN; + g_systemUsage = false; g_vibratorParameter.intensity = INTENSITY_ADJUST_MAX; g_vibratorParameter.frequency = 0; if (ret != ERR_OK) { @@ -252,9 +258,11 @@ int32_t PlayPrimitiveEffect(const char *effectId, int32_t intensity) MISC_HILOGD("Time delay measurement:start time"); CHKPR(effectId, PARAMETER_ERROR); auto &client = VibratorServiceClient::GetInstance(); - int32_t ret = client.PlayPrimitiveEffect(DEFAULT_VIBRATOR_ID, effectId, intensity, g_usage, g_loopCount); + int32_t ret = client.PlayPrimitiveEffect(DEFAULT_VIBRATOR_ID, effectId, intensity, + g_usage, g_systemUsage, g_loopCount); g_loopCount = 1; g_usage = USAGE_UNKNOWN; + g_systemUsage = false; if (ret != ERR_OK) { MISC_HILOGD("Play primitive effect failed, ret:%{public}d", ret); return NormalizeErrCode(ret); diff --git a/interfaces/inner_api/vibrator/vibrator_agent.h b/interfaces/inner_api/vibrator/vibrator_agent.h index 97a43d7bf81178b0582c02c64f3935e452ebca83..eb5b1314fa5e13afa3d5e110d6d60f4f1aff303e 100644 --- a/interfaces/inner_api/vibrator/vibrator_agent.h +++ b/interfaces/inner_api/vibrator/vibrator_agent.h @@ -129,7 +129,7 @@ int32_t Cancel(); * * @since 9 */ -bool SetUsage(int32_t usage); +bool SetUsage(int32_t usage, bool systemUsage = false); /** * @brief Query whether the HdHaptic is supported. diff --git a/services/miscdevice_service/BUILD.gn b/services/miscdevice_service/BUILD.gn index dddbe336c22a0f2cb4e0e20e658df80a692e5488..e3ae44e2c98be62e25ab7e54027a70b6bef82333 100644 --- a/services/miscdevice_service/BUILD.gn +++ b/services/miscdevice_service/BUILD.gn @@ -63,6 +63,7 @@ ohos_shared_library("libmiscdevice_service") { "ability_runtime:dataobs_manager", "access_token:libaccesstoken_sdk", "access_token:libprivacy_sdk", + "access_token:libtokenid_sdk", "bundle_framework:appexecfwk_core", "cJSON:cjson", "c_utils:utils", @@ -153,6 +154,7 @@ ohos_static_library("libmiscdevice_service_static") { "ability_runtime:dataobs_manager", "access_token:libaccesstoken_sdk", "access_token:libprivacy_sdk", + "access_token:libtokenid_sdk", "bundle_framework:appexecfwk_core", "cJSON:cjson", "c_utils:utils", diff --git a/services/miscdevice_service/include/miscdevice_service.h b/services/miscdevice_service/include/miscdevice_service.h index 0551a68e8904ef1eb2d3e77be5709e3f3431e7e4..d30d3f95889f9fd803f770aea71e2e649fef68c2 100644 --- a/services/miscdevice_service/include/miscdevice_service.h +++ b/services/miscdevice_service/include/miscdevice_service.h @@ -63,12 +63,12 @@ public: bool IsLightAnimationValid(const LightAnimationIPC &animation); int32_t Dump(int32_t fd, const std::vector &args) override; void ProcessDeathObserver(const wptr &object); - virtual int32_t Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage) override; + virtual int32_t Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage, bool systemUsage) override; virtual int32_t PlayVibratorEffect(int32_t vibratorId, const std::string &effect, - int32_t loopCount, int32_t usage) override; + int32_t loopCount, int32_t usage, bool systemUsage) override; #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM virtual int32_t PlayVibratorCustom(int32_t vibratorId, const RawFileDescriptor &rawFd, int32_t usage, - const VibrateParameter ¶meter) override; + bool systemUsage, const VibrateParameter ¶meter) override; #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM virtual int32_t StopVibrator(int32_t vibratorId) override; virtual int32_t StopVibrator(int32_t vibratorId, const std::string &mode) override; @@ -77,11 +77,11 @@ public: virtual int32_t TurnOn(int32_t lightId, const LightColor &color, const LightAnimationIPC &animation) override; virtual int32_t TurnOff(int32_t lightId) override; virtual int32_t PlayPattern(const VibratePattern &pattern, int32_t usage, - const VibrateParameter ¶meter) override; + bool systemUsage, const VibrateParameter ¶meter) override; virtual int32_t GetDelayTime(int32_t &delayTime) override; virtual int32_t TransferClientRemoteObject(const sptr &vibratorServiceClient) override; virtual int32_t PlayPrimitiveEffect(int32_t vibratorId, const std::string &effect, int32_t intensity, - int32_t usage, int32_t count) override; + int32_t usage, bool systemUsage, int32_t count) override; virtual int32_t GetVibratorCapacity(VibratorCapacity &capacity) override; private: diff --git a/services/miscdevice_service/include/vibration_priority_manager.h b/services/miscdevice_service/include/vibration_priority_manager.h index ab08dd8c2fe637b08f7991f38aa1f9256bd06273..726a48aec4c4f17ea6cb92f75302e8cd78f70ad1 100644 --- a/services/miscdevice_service/include/vibration_priority_manager.h +++ b/services/miscdevice_service/include/vibration_priority_manager.h @@ -81,6 +81,8 @@ private: bool ReleaseDataShareHelper(std::shared_ptr &helper); sptr CreateObserver(const MiscDeviceObserver::UpdateFunc &func); void UpdateStatus(); + bool IsSystemServiceCalling(); + bool IsSystemCalling(); sptr remoteObj_ { nullptr }; sptr observer_ { nullptr }; std::shared_ptr appMgrClientPtr_ {nullptr}; diff --git a/services/miscdevice_service/src/miscdevice_service.cpp b/services/miscdevice_service/src/miscdevice_service.cpp index a27d3ef82861e80b909a1552bc6a3c78a627142d..e1afa64118507997c4ba7e1f23adcde71f20d19e 100644 --- a/services/miscdevice_service/src/miscdevice_service.cpp +++ b/services/miscdevice_service/src/miscdevice_service.cpp @@ -310,7 +310,7 @@ bool MiscdeviceService::ShouldIgnoreVibrate(const VibrateInfo &info) return (PriorityManager->ShouldIgnoreVibrate(info, vibratorThread_) != VIBRATION); } -int32_t MiscdeviceService::Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage) +int32_t MiscdeviceService::Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage, bool systemUsage) { if ((timeOut <= MIN_VIBRATOR_TIME) || (timeOut > MAX_VIBRATOR_TIME) || (usage >= USAGE_MAX) || (usage < 0)) { @@ -323,6 +323,7 @@ int32_t MiscdeviceService::Vibrate(int32_t vibratorId, int32_t timeOut, int32_t .pid = GetCallingPid(), .uid = GetCallingUid(), .usage = usage, + .systemUsage = systemUsage, .duration = timeOut }; std::lock_guard lock(vibratorThreadMutex_); @@ -366,7 +367,7 @@ int32_t MiscdeviceService::StopVibrator(int32_t vibratorId) } int32_t MiscdeviceService::PlayVibratorEffect(int32_t vibratorId, const std::string &effect, - int32_t count, int32_t usage) + int32_t count, int32_t usage, bool systemUsage) { if ((count < MIN_VIBRATOR_COUNT) || (count > MAX_VIBRATOR_COUNT) || (usage >= USAGE_MAX) || (usage < 0)) { MISC_HILOGE("Invalid parameter"); @@ -387,6 +388,7 @@ int32_t MiscdeviceService::PlayVibratorEffect(int32_t vibratorId, const std::str .pid = GetCallingPid(), .uid = GetCallingUid(), .usage = usage, + .systemUsage = systemUsage, .duration = effectInfo->duration, .effect = effect, .count = count, @@ -483,7 +485,7 @@ std::string MiscdeviceService::GetCurrentTime() #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM int32_t MiscdeviceService::PlayVibratorCustom(int32_t vibratorId, const RawFileDescriptor &rawFd, int32_t usage, - const VibrateParameter ¶meter) + bool systemUsage, const VibrateParameter ¶meter) { if (!(g_capacity.isSupportHdHaptic || g_capacity.isSupportPresetMapping || g_capacity.isSupportTimeDelay)) { MISC_HILOGE("The device does not support this operation"); @@ -510,6 +512,7 @@ int32_t MiscdeviceService::PlayVibratorCustom(int32_t vibratorId, const RawFileD .pid = GetCallingPid(), .uid = GetCallingUid(), .usage = usage, + .systemUsage = systemUsage, .package = package, }; if (g_capacity.isSupportHdHaptic) { @@ -645,7 +648,7 @@ int32_t MiscdeviceService::Dump(int32_t fd, const std::vector &a } int32_t MiscdeviceService::PlayPattern(const VibratePattern &pattern, int32_t usage, - const VibrateParameter ¶meter) + bool systemUsage, const VibrateParameter ¶meter) { if ((usage >= USAGE_MAX) || (usage < 0) || (!CheckVibratorParmeters(parameter))) { MISC_HILOGE("Invalid parameter, usage:%{public}d", usage); @@ -667,6 +670,7 @@ int32_t MiscdeviceService::PlayPattern(const VibratePattern &pattern, int32_t us .pid = GetCallingPid(), .uid = GetCallingUid(), .usage = usage, + .systemUsage = systemUsage }; if (g_capacity.isSupportHdHaptic) { std::lock_guard lock(vibratorThreadMutex_); @@ -848,7 +852,7 @@ void MiscdeviceService::DestroyClientPid(const sptr &vibratorServ } int32_t MiscdeviceService::PlayPrimitiveEffect(int32_t vibratorId, const std::string &effect, - int32_t intensity, int32_t usage, int32_t count) + int32_t intensity, int32_t usage, bool systemUsage, int32_t count) { if ((intensity <= INTENSITY_MIN) || (intensity > INTENSITY_MAX) || (usage >= USAGE_MAX) || (usage < 0)) { MISC_HILOGE("Invalid parameter"); @@ -869,6 +873,7 @@ int32_t MiscdeviceService::PlayPrimitiveEffect(int32_t vibratorId, const std::st .pid = GetCallingPid(), .uid = GetCallingUid(), .usage = usage, + .systemUsage = systemUsage, .duration = effectInfo->duration, .effect = effect, .count = count, diff --git a/services/miscdevice_service/src/miscdevice_service_stub.cpp b/services/miscdevice_service/src/miscdevice_service_stub.cpp index f08e120d15d4710f1bd3b36ab36e1acb386396a4..606630b0cde54a7c0651e5026b9e5ff51aa22bcb 100644 --- a/services/miscdevice_service/src/miscdevice_service_stub.cpp +++ b/services/miscdevice_service/src/miscdevice_service_stub.cpp @@ -55,11 +55,13 @@ int32_t MiscdeviceServiceStub::VibrateStub(MessageParcel &data, MessageParcel &r int32_t vibratorId; int32_t duration; int32_t usage; - if ((!data.ReadInt32(vibratorId)) || (!data.ReadInt32(duration)) || (!data.ReadInt32(usage))) { + bool systemUsage; + if ((!data.ReadInt32(vibratorId)) || (!data.ReadInt32(duration)) || (!data.ReadInt32(usage))|| + (!data.ReadBool(systemUsage))) { MISC_HILOGE("Parcel read failed"); return ERROR; } - return Vibrate(vibratorId, duration, usage); + return Vibrate(vibratorId, duration, usage, systemUsage); } int32_t MiscdeviceServiceStub::StopVibratorAllStub(MessageParcel &data, MessageParcel &reply) @@ -94,12 +96,13 @@ int32_t MiscdeviceServiceStub::PlayVibratorEffectStub(MessageParcel &data, Messa std::string effect; int32_t count; int32_t usage; + bool systemUsage; if ((!data.ReadInt32(vibratorId)) || (!data.ReadString(effect)) || - (!data.ReadInt32(count)) || (!data.ReadInt32(usage))) { + (!data.ReadInt32(count)) || (!data.ReadInt32(usage)) || (!data.ReadBool(systemUsage))) { MISC_HILOGE("Parcel read failed"); return ERROR; } - return PlayVibratorEffect(vibratorId, effect, count, usage); + return PlayVibratorEffect(vibratorId, effect, count, usage, systemUsage); } int32_t MiscdeviceServiceStub::StopVibratorByModeStub(MessageParcel &data, MessageParcel &reply) @@ -157,8 +160,9 @@ int32_t MiscdeviceServiceStub::PlayVibratorCustomStub(MessageParcel &data, Messa return ERROR; } int32_t usage; - if (!data.ReadInt32(usage)) { - MISC_HILOGE("Parcel read usage failed"); + bool systemUsage; + if (!data.ReadInt32(usage) || !data.ReadBool(systemUsage)) { + MISC_HILOGE("Parcel read failed"); return ERROR; } VibrateParameter vibrateParameter; @@ -181,7 +185,7 @@ int32_t MiscdeviceServiceStub::PlayVibratorCustomStub(MessageParcel &data, Messa MISC_HILOGE("Parcel ReadFileDescriptor failed"); return ERROR; } - ret = PlayVibratorCustom(vibratorId, rawFd, usage, parameter.value()); + ret = PlayVibratorCustom(vibratorId, rawFd, usage, systemUsage, parameter.value()); close(rawFd.fd); if (ret != ERR_OK) { MISC_HILOGD("PlayVibratorCustom failed, ret:%{public}d", ret); @@ -333,13 +337,18 @@ int32_t MiscdeviceServiceStub::PlayPatternStub(MessageParcel &data, MessageParce MISC_HILOGE("Parcel read usage failed"); return ERROR; } + bool systemUsage = false; + if (!data.ReadBool(systemUsage)) { + MISC_HILOGE("Parcel read systemUsage failed"); + return ERROR; + } VibrateParameter vibrateParameter; auto parameter = vibrateParameter.Unmarshalling(data); if (!parameter.has_value()) { MISC_HILOGE("Parameter Unmarshalling failed"); return ERROR; } - return PlayPattern(pattern.value(), usage, parameter.value()); + return PlayPattern(pattern.value(), usage, systemUsage, parameter.value()); } int32_t MiscdeviceServiceStub::GetDelayTimeStub(MessageParcel &data, MessageParcel &reply) @@ -382,13 +391,15 @@ int32_t MiscdeviceServiceStub::PlayPrimitiveEffectStub(MessageParcel &data, Mess std::string effect; int32_t intensity = 0; int32_t usage = 0; + bool systemUsage = false; int32_t count = 0; if ((!data.ReadInt32(vibratorId)) || (!data.ReadString(effect)) || - (!data.ReadInt32(intensity)) || (!data.ReadInt32(usage)) || (!data.ReadInt32(count))) { + (!data.ReadInt32(intensity)) || (!data.ReadInt32(usage)) || (!data.ReadBool(systemUsage)) || + (!data.ReadInt32(count))) { MISC_HILOGE("Parcel read failed"); return ERROR; } - return PlayPrimitiveEffect(vibratorId, effect, intensity, usage, count); + return PlayPrimitiveEffect(vibratorId, effect, intensity, usage, systemUsage, count); } int32_t MiscdeviceServiceStub::GetVibratorCapacityStub(MessageParcel &data, MessageParcel &reply) diff --git a/services/miscdevice_service/src/vibration_priority_manager.cpp b/services/miscdevice_service/src/vibration_priority_manager.cpp index 897b24834f18b9d95e666173012c3e9747d1c42a..29c494518ff1f17a033e424b87f7677528684df3 100644 --- a/services/miscdevice_service/src/vibration_priority_manager.cpp +++ b/services/miscdevice_service/src/vibration_priority_manager.cpp @@ -15,6 +15,9 @@ #include "vibration_priority_manager.h" +#include + +#include "accesstoken_kit.h" #include "bundle_mgr_client.h" #include "ipc_skeleton.h" #include "iservice_registry.h" @@ -171,6 +174,25 @@ void VibrationPriorityManager::UpdateStatus() } } +bool VibrationPriorityManager::IsSystemServiceCalling() +{ + const auto tokenId = IPCSkeleton::GetCallingTokenID(); + const auto flag = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId); + if (flag == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE) { + MISC_HILOGD("System service calling, flag: %{public}u", flag); + return true; + } + return false; +} + +bool VibrationPriorityManager::IsSystemCalling() +{ + if (IsSystemServiceCalling()) { + return true; + } + return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(IPCSkeleton::GetCallingFullTokenID()); +} + bool VibrationPriorityManager::ShouldIgnoreInputMethod(const VibrateInfo &vibrateInfo) { int32_t pid = vibrateInfo.pid; @@ -226,17 +248,19 @@ VibrateStatus VibrationPriorityManager::ShouldIgnoreVibrate(const VibrateInfo &v return VIBRATION; } UpdateStatus(); - if ((vibrateInfo.usage == USAGE_ALARM || vibrateInfo.usage == USAGE_RING || - vibrateInfo.usage == USAGE_NOTIFICATION - || vibrateInfo.usage == USAGE_COMMUNICATION) && (miscAudioRingerMode_ == RINGER_MODE_SILENT)) { - MISC_HILOGD("Vibration is ignored for ringer mode:%{public}d", static_cast(miscAudioRingerMode_)); - return IGNORE_RINGER_MODE; - } - if (((vibrateInfo.usage == USAGE_TOUCH || vibrateInfo.usage == USAGE_MEDIA || vibrateInfo.usage == USAGE_UNKNOWN - || vibrateInfo.usage == USAGE_PHYSICAL_FEEDBACK || vibrateInfo.usage == USAGE_SIMULATE_REALITY) - && (miscFeedback_ == FEEDBACK_MODE_OFF)) && !ShouldIgnoreInputMethod(vibrateInfo)) { - MISC_HILOGD("Vibration is ignored for feedback:%{public}d", static_cast(miscFeedback_)); - return IGNORE_FEEDBACK; + if (!IsSystemCalling() || vibrateInfo.systemUsage == false) { + if ((vibrateInfo.usage == USAGE_ALARM || vibrateInfo.usage == USAGE_RING + || vibrateInfo.usage == USAGE_NOTIFICATION || vibrateInfo.usage == USAGE_COMMUNICATION) + && (miscAudioRingerMode_ == RINGER_MODE_SILENT)) { + MISC_HILOGD("Vibration is ignored for ringer mode:%{public}d", static_cast(miscAudioRingerMode_)); + return IGNORE_RINGER_MODE; + } + if (((vibrateInfo.usage == USAGE_TOUCH || vibrateInfo.usage == USAGE_MEDIA || vibrateInfo.usage == USAGE_UNKNOWN + || vibrateInfo.usage == USAGE_PHYSICAL_FEEDBACK || vibrateInfo.usage == USAGE_SIMULATE_REALITY) + && (miscFeedback_ == FEEDBACK_MODE_OFF)) && !ShouldIgnoreInputMethod(vibrateInfo)) { + MISC_HILOGD("Vibration is ignored for feedback:%{public}d", static_cast(miscFeedback_)); + return IGNORE_FEEDBACK; + } } if (vibratorThread == nullptr) { MISC_HILOGD("There is no vibration, it can vibrate"); diff --git a/utils/common/include/vibrator_infos.h b/utils/common/include/vibrator_infos.h index a0e576ffa168635acdf7c190400c09f26fbe7631..47f820f9265049d99aa8f3a3593b74171d2c47d4 100644 --- a/utils/common/include/vibrator_infos.h +++ b/utils/common/include/vibrator_infos.h @@ -124,6 +124,7 @@ struct VibrateInfo { int32_t pid = -1; int32_t uid = -1; int32_t usage = 0; + bool systemUsage = false; int32_t duration = 0; std::string effect; int32_t count = 0;