From d7f318eae7ace3e0cfe0cffb3a099aac8b8dddaf Mon Sep 17 00:00:00 2001 From: wuzhihuitmac Date: Sat, 13 Jan 2024 02:33:57 +0000 Subject: [PATCH] Ringer mode and feedback changed from listening to querying the database Signed-off-by: wuzhihuitmac Change-Id: I7f755e26c10b05a8e9fde5b079eb095c242d4a9a --- bundle.json | 1 - services/miscdevice_service/BUILD.gn | 2 - .../include/miscdevice_observer.h | 36 ----- .../include/vibration_priority_manager.h | 8 +- .../src/miscdevice_observer.cpp | 35 ----- .../src/vibration_priority_manager.cpp | 146 ++++-------------- 6 files changed, 28 insertions(+), 200 deletions(-) delete mode 100644 services/miscdevice_service/include/miscdevice_observer.h delete mode 100644 services/miscdevice_service/src/miscdevice_observer.cpp diff --git a/bundle.json b/bundle.json index 4243f24..6375d93 100644 --- a/bundle.json +++ b/bundle.json @@ -24,7 +24,6 @@ "ability_base", "ability_runtime", "access_token", - "audio_framework", "c_utils", "data_share", "drivers_interface_light", diff --git a/services/miscdevice_service/BUILD.gn b/services/miscdevice_service/BUILD.gn index f90ba6b..bd68d25 100644 --- a/services/miscdevice_service/BUILD.gn +++ b/services/miscdevice_service/BUILD.gn @@ -22,7 +22,6 @@ ohos_shared_library("libmiscdevice_service") { "hdi_connection/interface/src/light_hdi_connection.cpp", "hdi_connection/interface/src/vibrator_hdi_connection.cpp", "src/miscdevice_dump.cpp", - "src/miscdevice_observer.cpp", "src/miscdevice_service.cpp", "src/miscdevice_service_stub.cpp", "src/vibration_priority_manager.cpp", @@ -98,7 +97,6 @@ ohos_shared_library("libmiscdevice_service_static") { "hdi_connection/interface/src/light_hdi_connection.cpp", "hdi_connection/interface/src/vibrator_hdi_connection.cpp", "src/miscdevice_dump.cpp", - "src/miscdevice_observer.cpp", "src/miscdevice_service.cpp", "src/miscdevice_service_stub.cpp", "src/vibration_priority_manager.cpp", diff --git a/services/miscdevice_service/include/miscdevice_observer.h b/services/miscdevice_service/include/miscdevice_observer.h deleted file mode 100644 index f043762..0000000 --- a/services/miscdevice_service/include/miscdevice_observer.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MISCDEVICE_OBSERVER_H -#define MISCDEVICE_OBSERVER_H - -#include "data_ability_observer_stub.h" - -namespace OHOS { -namespace Sensors { -class MiscDeviceObserver : public AAFwk::DataAbilityObserverStub { -public: - MiscDeviceObserver(); - ~MiscDeviceObserver() override; - void OnChange() override; - - using UpdateFunc = std::function; - void SetUpdateFunc(const UpdateFunc &func); -private: - UpdateFunc update_ = nullptr; -}; -} // namespace Sensors -} // namespace OHOS -#endif // MISCDEVICE_OBSERVER_H diff --git a/services/miscdevice_service/include/vibration_priority_manager.h b/services/miscdevice_service/include/vibration_priority_manager.h index 374cf1a..5bc5660 100644 --- a/services/miscdevice_service/include/vibration_priority_manager.h +++ b/services/miscdevice_service/include/vibration_priority_manager.h @@ -25,7 +25,6 @@ #include "iremote_object.h" #include "singleton.h" -#include "miscdevice_observer.h" #include "vibrator_infos.h" #include "vibrator_thread.h" @@ -67,22 +66,19 @@ private: bool IsCurrentVibrate(std::shared_ptr vibratorThread) const; bool IsLoopVibrate(const VibrateInfo &vibrateInfo) const; VibrateStatus ShouldIgnoreVibrate(const VibrateInfo &vibrateInfo, VibrateInfo currentVibrateInfo) const; - static void ExecRegisterCb(const sptr &observer); - int32_t RegisterObserver(const sptr &observer); - int32_t UnregisterObserver(const sptr &observer); int32_t GetIntValue(const std::string &key, int32_t &value); int32_t GetLongValue(const std::string &key, int64_t &value); int32_t GetStringValue(const std::string &key, std::string &value); Uri AssembleUri(const std::string &key); std::shared_ptr CreateDataShareHelper(); bool ReleaseDataShareHelper(std::shared_ptr &helper); - sptr CreateObserver(const MiscDeviceObserver::UpdateFunc &func); void Initialize(); void UpdateStatus(); sptr remoteObj_ { nullptr }; - sptr observer_ { nullptr }; std::atomic_int32_t miscFeedback_ = FEEDBACK_MODE_INVALID; std::atomic_int32_t miscAudioRingerMode_ = RINGER_MODE_INVALID; + std::shared_ptr helper_ = nullptr; + std::string callingIdentity_; }; #define PriorityManager DelayedSingleton::GetInstance() } // namespace Sensors diff --git a/services/miscdevice_service/src/miscdevice_observer.cpp b/services/miscdevice_service/src/miscdevice_observer.cpp deleted file mode 100644 index 2e18f96..0000000 --- a/services/miscdevice_service/src/miscdevice_observer.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "miscdevice_observer.h" - -namespace OHOS { -namespace Sensors { -MiscDeviceObserver::MiscDeviceObserver() = default; -MiscDeviceObserver::~MiscDeviceObserver() = default; - -void MiscDeviceObserver::OnChange() -{ - if (update_ != nullptr) { - update_(); - } -} - -void MiscDeviceObserver::SetUpdateFunc(const UpdateFunc &func) -{ - update_ = func; -} -} // namespace Sensors -} // namespace OHOS \ No newline at end of file diff --git a/services/miscdevice_service/src/vibration_priority_manager.cpp b/services/miscdevice_service/src/vibration_priority_manager.cpp index c8fa769..a5882ef 100644 --- a/services/miscdevice_service/src/vibration_priority_manager.cpp +++ b/services/miscdevice_service/src/vibration_priority_manager.cpp @@ -38,37 +38,12 @@ constexpr int32_t DECEM_BASE = 10; VibrationPriorityManager::VibrationPriorityManager() { Initialize(); - MiscDeviceObserver::UpdateFunc updateFunc = [&]() { - int32_t feedback = miscFeedback_; - if (GetIntValue(SETTING_FEEDBACK_KEY, feedback) != ERR_OK) { - MISC_HILOGE("Get feedback failed"); - } - miscFeedback_ = feedback; - MISC_HILOGI("feedback:%{public}d", feedback); - int32_t ringerMode = miscAudioRingerMode_; - if (GetIntValue(SETTING_RINGER_MODE_KEY, ringerMode) != ERR_OK) { - MISC_HILOGE("Get ringerMode failed"); - } - miscAudioRingerMode_ = ringerMode; - MISC_HILOGI("ringerMode:%{public}d", ringerMode); - }; - auto observer = CreateObserver(updateFunc); - if (observer == nullptr) { - MISC_HILOGE("observer is null"); - return; - } - observer_ = observer; - if (RegisterObserver(observer_) != ERR_OK) { - MISC_HILOGE("RegisterObserver failed"); - } } VibrationPriorityManager::~VibrationPriorityManager() { remoteObj_ = nullptr; - if (UnregisterObserver(observer_) != ERR_OK) { - MISC_HILOGE("UnregisterObserver failed"); - } + ReleaseDataShareHelper(helper_); } int32_t VibrationPriorityManager::GetIntValue(const std::string &key, int32_t &value) @@ -95,28 +70,25 @@ int32_t VibrationPriorityManager::GetLongValue(const std::string &key, int64_t & int32_t VibrationPriorityManager::GetStringValue(const std::string &key, std::string &value) { - std::string callingIdentity = IPCSkeleton::ResetCallingIdentity(); - auto helper = CreateDataShareHelper(); - if (helper == nullptr) { - IPCSkeleton::SetCallingIdentity(callingIdentity); - return MISC_NO_INIT_ERR; - } std::vector columns = {SETTING_COLUMN_VALUE}; DataShare::DataSharePredicates predicates; predicates.EqualTo(SETTING_COLUMN_KEYWORD, key); Uri uri(AssembleUri(key)); - auto resultSet = helper->Query(uri, predicates, columns); - ReleaseDataShareHelper(helper); + if (helper_ == nullptr) { + IPCSkeleton::SetCallingIdentity(callingIdentity_); + return MISC_NO_INIT_ERR; + } + auto resultSet = helper_->Query(uri, predicates, columns); if (resultSet == nullptr) { MISC_HILOGE("resultSet is nullptr"); - IPCSkeleton::SetCallingIdentity(callingIdentity); + IPCSkeleton::SetCallingIdentity(callingIdentity_); return MISC_INVALID_OPERATION_ERR; } int32_t count; resultSet->GetRowCount(count); if (count == 0) { MISC_HILOGW("Not found value, key:%{public}s, count:%{public}d", key.c_str(), count); - IPCSkeleton::SetCallingIdentity(callingIdentity); + IPCSkeleton::SetCallingIdentity(callingIdentity_); return MISC_NAME_NOT_FOUND_ERR; } const int32_t index = 0; @@ -124,32 +96,28 @@ int32_t VibrationPriorityManager::GetStringValue(const std::string &key, std::st int32_t ret = resultSet->GetString(index, value); if (ret != ERR_OK) { MISC_HILOGW("GetString failed, ret:%{public}d", ret); - IPCSkeleton::SetCallingIdentity(callingIdentity); + IPCSkeleton::SetCallingIdentity(callingIdentity_); return ERROR; } resultSet->Close(); - IPCSkeleton::SetCallingIdentity(callingIdentity); + IPCSkeleton::SetCallingIdentity(callingIdentity_); return ERR_OK; } void VibrationPriorityManager::UpdateStatus() { - if (miscFeedback_ == FEEDBACK_MODE_INVALID) { - int32_t feedback = FEEDBACK_MODE_INVALID; - if (GetIntValue(SETTING_FEEDBACK_KEY, feedback) != ERR_OK) { - feedback = FEEDBACK_MODE_ON; - MISC_HILOGE("Get feedback failed"); - } - miscFeedback_ = feedback; + int32_t feedback = FEEDBACK_MODE_INVALID; + if (GetIntValue(SETTING_FEEDBACK_KEY, feedback) != ERR_OK) { + feedback = FEEDBACK_MODE_ON; + MISC_HILOGE("Get feedback failed"); } - if (miscAudioRingerMode_ == RINGER_MODE_INVALID) { - int32_t ringerMode = RINGER_MODE_INVALID; - if (GetIntValue(SETTING_RINGER_MODE_KEY, ringerMode) != ERR_OK) { - ringerMode = RINGER_MODE_NORMAL; - MISC_HILOGE("Get ringerMode failed"); - } - miscAudioRingerMode_ = ringerMode; + miscFeedback_ = feedback; + int32_t ringerMode = RINGER_MODE_INVALID; + if (GetIntValue(SETTING_RINGER_MODE_KEY, ringerMode) != ERR_OK) { + ringerMode = RINGER_MODE_NORMAL; + MISC_HILOGE("Get ringerMode failed"); } + miscAudioRingerMode_ = ringerMode; } VibrateStatus VibrationPriorityManager::ShouldIgnoreVibrate(const VibrateInfo &vibrateInfo, @@ -218,17 +186,6 @@ VibrateStatus VibrationPriorityManager::ShouldIgnoreVibrate(const VibrateInfo &v return VIBRATION; } -sptr VibrationPriorityManager::CreateObserver(const MiscDeviceObserver::UpdateFunc &func) -{ - sptr observer = new MiscDeviceObserver(); - if (observer == nullptr) { - MISC_HILOGE("observer is null"); - return observer; - } - observer->SetUpdateFunc(func); - return observer; -} - void VibrationPriorityManager::Initialize() { auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); @@ -242,6 +199,12 @@ void VibrationPriorityManager::Initialize() return; } remoteObj_ = remoteObj; + callingIdentity_ = IPCSkeleton::ResetCallingIdentity(); + helper_ = CreateDataShareHelper(); + if (helper_ == nullptr) { + IPCSkeleton::SetCallingIdentity(callingIdentity_); + MISC_HILOGE("CreateDataShareHelper failed"); + } } Uri VibrationPriorityManager::AssembleUri(const std::string &key) @@ -272,62 +235,5 @@ bool VibrationPriorityManager::ReleaseDataShareHelper(std::shared_ptr &observer) -{ - if (observer == nullptr) { - MISC_HILOGE("observer is nullptr"); - return; - } - observer->OnChange(); -} - -int32_t VibrationPriorityManager::RegisterObserver(const sptr &observer) -{ - if (observer == nullptr) { - MISC_HILOGE("observer is nullptr"); - return MISC_NO_INIT_ERR; - } - std::string callingIdentity = IPCSkeleton::ResetCallingIdentity(); - auto helper = CreateDataShareHelper(); - if (helper == nullptr) { - IPCSkeleton::SetCallingIdentity(callingIdentity); - return MISC_NO_INIT_ERR; - } - auto uriFeedback = AssembleUri(SETTING_FEEDBACK_KEY); - helper->RegisterObserver(uriFeedback, observer); - helper->NotifyChange(uriFeedback); - auto uriRingerMode = AssembleUri(SETTING_RINGER_MODE_KEY); - helper->RegisterObserver(uriRingerMode, observer); - helper->NotifyChange(uriRingerMode); - std::thread execCb(VibrationPriorityManager::ExecRegisterCb, observer); - execCb.detach(); - ReleaseDataShareHelper(helper); - IPCSkeleton::SetCallingIdentity(callingIdentity); - MISC_HILOGD("succeed to register observer of uri"); - return ERR_OK; -} - -int32_t VibrationPriorityManager::UnregisterObserver(const sptr &observer) -{ - if (observer == nullptr) { - MISC_HILOGE("observer is nullptr"); - return MISC_NO_INIT_ERR; - } - std::string callingIdentity = IPCSkeleton::ResetCallingIdentity(); - auto helper = CreateDataShareHelper(); - if (helper == nullptr) { - IPCSkeleton::SetCallingIdentity(callingIdentity); - return MISC_NO_INIT_ERR; - } - auto uriFeedback = AssembleUri(SETTING_FEEDBACK_KEY); - helper->UnregisterObserver(uriFeedback, observer); - auto uriRingerMode = AssembleUri(SETTING_RINGER_MODE_KEY); - helper->UnregisterObserver(uriRingerMode, observer); - ReleaseDataShareHelper(helper); - IPCSkeleton::SetCallingIdentity(callingIdentity); - MISC_HILOGD("succeed to unregister observer"); - return ERR_OK; -} } // namespace Sensors } // namespace OHOS \ No newline at end of file -- Gitee