辅助应用安装卸载事件监听

Signed-off-by: zhupengfei <zhupengfei26@huawei.com>
This commit is contained in:
zhupengfei 2024-03-11 21:37:11 +08:00
parent f91b0bf235
commit 2d3c720a30
17 changed files with 195 additions and 11 deletions

View File

@ -38,6 +38,11 @@ public:
* @brief Receive the state notify from AAMS and send it to the observer registered.
*/
virtual void OnAccessibilityEnableAbilityListsChanged() override;
/**
* @brief Receive the install state notify from AAMS and send it to the observer registered.
*/
virtual void OnAccessibilityInstallAbilityListsChanged() override;
private:
/**
* @brief Write the descriptor of IPC.

View File

@ -52,6 +52,14 @@ private:
* @return NO_ERROR: successful; otherwise is failed.
*/
ErrCode HandleOnAccessibilityEnableAbilityListsChanged(MessageParcel &data, MessageParcel &reply);
/**
* @brief Handle the IPC request for the function:HandleOnInstallStateChanged.
* @param data The data of process communication
* @param reply The response of IPC request
* @return NO_ERROR: successful; otherwise is failed.
*/
ErrCode HandleOnAccessibilityInstallAbilityListsChanged(MessageParcel &data, MessageParcel &reply);
};
} // namespace Accessibility
} // namespace OHOS

View File

@ -34,6 +34,7 @@ namespace Accessibility {
OUTSIDE_TOUCH,
ON_ACCESSIBILITY_ENABLE_ABILITY_LISTS_CHANGED = 300,
ON_ACCESSIBILITY_INSTALL_ABILITY_LISTS_CHANGED,
SEARCH_ELEMENTINFO_BY_ACCESSIBILITY_ID = 400,
SEARCH_ELEMENTINFOS_BY_TEXT,

View File

@ -28,6 +28,11 @@ public:
* @brief Receive the state notify from AAMS and send it to the observer registered.
*/
virtual void OnAccessibilityEnableAbilityListsChanged() = 0;
/**
* @brief Receive the isntall state notify from AAMS and send it to the observer registered.
*/
virtual void OnAccessibilityInstallAbilityListsChanged() = 0;
};
} // namespace Accessibility
} // namespace OHOS

View File

@ -62,5 +62,32 @@ void AccessibilityEnableAbilityListsObserverProxy::OnAccessibilityEnableAbilityL
HILOG_ERROR("OnAccessibilityEnableAbilityListsChanged fail, error: %{public}d", result);
}
}
void AccessibilityEnableAbilityListsObserverProxy::OnAccessibilityInstallAbilityListsChanged()
{
HILOG_DEBUG();
MessageParcel data;
MessageParcel reply;
MessageOption option(MessageOption::TF_ASYNC);
if (!WriteInterfaceToken(data)) {
HILOG_ERROR("write interface token failed");
return;
}
sptr<IRemoteObject> remote = Remote();
if (!remote) {
HILOG_ERROR("fail to send transact cmd due to remote object");
return;
}
int32_t result = remote->SendRequest(static_cast<uint32_t>(
AccessibilityInterfaceCode::ON_ACCESSIBILITY_INSTALL_ABILITY_LISTS_CHANGED),
data, reply, option);
if (result != NO_ERROR) {
HILOG_ERROR("OnAccessibilityInstallAbilityListsChanged fail, error: %{public}d", result);
}
}
} // namespace Accessibility
} // namespace OHOS

View File

@ -49,5 +49,13 @@ ErrCode AccessibilityEnableAbilityListsObserverStub::HandleOnAccessibilityEnable
OnAccessibilityEnableAbilityListsChanged();
return NO_ERROR;
}
ErrCode AccessibilityEnableAbilityListsObserverStub::HandleOnAccessibilityInstallAbilityListsChanged(
MessageParcel &data, MessageParcel &reply)
{
HILOG_DEBUG();
OnAccessibilityInstallAbilityListsChanged();
return NO_ERROR;
}
} // namespace Accessibility
} // namespace OHOS

View File

@ -31,6 +31,7 @@ public:
~EnableAbilityListsObserverTmplFuzzTest() = default;
void OnAccessibilityEnableAbilityListsChanged() override {};
void OnAccessibilityInstallAbilityListsChanged() override {};
};
template<class T>

View File

@ -101,6 +101,7 @@ public:
void OnAccessibleAbilityManagerClickResponseTimeChanged(const uint32_t clickResponseTime);
void OnAccessibleAbilityManagerIgnoreRepeatClickTimeChanged(const uint32_t ignoreRepeatClickTime);
void OnAccessibilityEnableAbilityListsChanged();
void OnAccessibilityInstallAbilityListsChanged();
private:
class AccessibilityEnableAbilityListsObserverImpl :
@ -114,6 +115,11 @@ private:
{
client_.OnAccessibilityEnableAbilityListsChanged();
}
virtual void OnAccessibilityInstallAbilityListsChanged() override
{
client_.OnAccessibilityInstallAbilityListsChanged();
}
private:
Impl &client_;
};

View File

@ -1346,6 +1346,23 @@ void AccessibilityConfig::Impl::OnAccessibilityEnableAbilityListsChanged()
}
}
void AccessibilityConfig::Impl::OnAccessibilityInstallAbilityListsChanged()
{
HILOG_DEBUG("observer's size is %{public}zu", enableAbilityListsObservers_.size());
std::vector<std::shared_ptr<AccessibilityEnableAbilityListsObserver>> observers;
{
std::lock_guard<std::mutex> lock(mutex_);
observers = enableAbilityListsObservers_;
}
for (auto &enableAbilityListsObserver : observers) {
if (enableAbilityListsObserver != nullptr) {
enableAbilityListsObserver->OnInstallAbilityListsStateChanged();
} else {
HILOG_ERROR("enableAbilityListsObserver is null");
}
}
}
void AccessibilityConfig::Impl::OnIgnoreRepeatClickStateChanged(const uint32_t stateType)
{
if (stateType & Accessibility::STATE_IGNORE_REPEAT_CLICK_ENABLED) {

View File

@ -67,6 +67,8 @@ public:
MockAccessibilityEnableAbilityListsObserverImpl() = default;
void OnEnableAbilityListsStateChanged() override
{}
void OnInstallAbilityListsStateChanged() override
{}
};
/**

View File

@ -96,6 +96,11 @@ public:
* @brief Called when the enable ability lists changed.
*/
virtual void OnEnableAbilityListsStateChanged() = 0;
/**
* @brief Called when the enable ability lists changed.
*/
virtual void OnInstallAbilityListsStateChanged() = 0;
};
class AccessibilityConfig {

View File

@ -42,6 +42,9 @@ public:
void OnEnableAbilityListsStateChanged() override
{
}
void OnInstallAbilityListsStateChanged() override
{
}
};
template<class T>

View File

@ -37,14 +37,19 @@ class EnableAbilityListsObserverImpl : public OHOS::AccessibilityConfig::Accessi
public:
EnableAbilityListsObserverImpl() = default;
void OnEnableAbilityListsStateChanged() override;
void OnInstallAbilityListsStateChanged() override;
void SubscribeToFramework();
void SubscribeObserver(napi_env env, napi_value observer);
void SubscribeInstallObserver(napi_env env, napi_value observer);
void UnsubscribeObserver(napi_env env, napi_value observer);
void UnsubscribeObservers();
void UnsubscribeInstallObserver(napi_env env, napi_value observer);
void UnsubscribeInstallObservers();
private:
std::mutex mutex_;
std::vector<std::shared_ptr<EnableAbilityListsObserver>> enableAbilityListsObservers_ = {};
std::vector<std::shared_ptr<EnableAbilityListsObserver>> installAbilityListsObservers_ = {};
};
struct NAccessibilityConfigData {

View File

@ -240,13 +240,14 @@ napi_value NAccessibilityConfig::SubscribeState(napi_env env, napi_callback_info
errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
}
std::string observerType = "";
if (errCode == OHOS::Accessibility::RET_OK) {
std::string observerType = "";
if (!ParseString(env, observerType, args[PARAM0])) {
HILOG_ERROR("observer type parse failed");
errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
} else {
if (std::strcmp(observerType.c_str(), "enabledAccessibilityExtensionListChange") != 0) {
if (std::strcmp(observerType.c_str(), "enabledAccessibilityExtensionListChange") != 0 ||
std::strcmp(observerType.c_str(), "installedAccessibilityListChange") != 0) {
HILOG_ERROR("args[PARAM0] is wrong[%{public}s", observerType.c_str());
errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
}
@ -268,8 +269,16 @@ napi_value NAccessibilityConfig::SubscribeState(napi_env env, napi_callback_info
napi_throw(env, err);
return nullptr;
}
enableAbilityListsObservers_->SubscribeObserver(env, args[PARAM1]);
if (enableAbilityListsObservers_ == nullptr) {
HILOG_ERROR("enableAbilityListsObservers_ is null");
return nullptr;
}
if (std::strcmp(observerType.c_str(), "enabledAccessibilityExtensionListChange") == 0) {
enableAbilityListsObservers_->SubscribeObserver(env, args[PARAM1]);
}
if (std::strcmp(observerType.c_str(), "installedAccessibilityListChange") == 0) {
enableAbilityListsObservers_->SubscribeInstallObserver(env, args[PARAM1]);
}
return nullptr;
}
@ -288,14 +297,14 @@ napi_value NAccessibilityConfig::UnsubscribeState(napi_env env, napi_callback_in
HILOG_ERROR("argc is invalid: %{public}zu", argc);
errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
}
std::string observerType = "";
if (errCode == OHOS::Accessibility::RET_OK) {
std::string observerType = "";
if (!ParseString(env, observerType, args[PARAM0])) {
HILOG_ERROR("observer type parse failed");
errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
} else {
if (std::strcmp(observerType.c_str(), "enabledAccessibilityExtensionListChange") != 0) {
if (std::strcmp(observerType.c_str(), "enabledAccessibilityExtensionListChange") != 0 ||
std::strcmp(observerType.c_str(), "installedAccessibilityListChange") != 0) {
HILOG_ERROR("args[PARAM0] is wrong[%{public}s", observerType.c_str());
errCode = OHOS::Accessibility::RET_ERR_INVALID_PARAM;
}
@ -308,11 +317,22 @@ napi_value NAccessibilityConfig::UnsubscribeState(napi_env env, napi_callback_in
napi_throw(env, err);
return nullptr;
}
if (enableAbilityListsObservers_ == nullptr) {
HILOG_ERROR("enableAbilityListsObservers_ is null");
return nullptr;
}
if (argc > ARGS_SIZE_TWO - 1 && CheckJsFunction(env, args[PARAM1])) {
enableAbilityListsObservers_->UnsubscribeObserver(env, args[PARAM1]);
if (std::strcmp(observerType.c_str(), "enabledAccessibilityExtensionListChange") == 0) {
enableAbilityListsObservers_->UnsubscribeObserver(env, args[PARAM1]);
} else {
enableAbilityListsObservers_->UnsubscribeInstallObserver(env, args[PARAM1]);
}
} else {
enableAbilityListsObservers_->UnsubscribeObservers();
if (std::strcmp(observerType.c_str(), "enabledAccessibilityExtensionListChange") == 0) {
enableAbilityListsObservers_->UnsubscribeObservers();
} else {
enableAbilityListsObservers_->UnsubscribeInstallObservers();
}
}
return nullptr;
}
@ -933,6 +953,19 @@ void EnableAbilityListsObserverImpl::OnEnableAbilityListsStateChanged()
}
}
void EnableAbilityListsObserverImpl::OnInstallAbilityListsStateChanged()
{
HILOG_DEBUG();
std::lock_guard<std::mutex> lock(mutex_);
for (auto &observer : installAbilityListsObservers_) {
if (observer) {
observer->OnEnableAbilityListsStateChanged();
} else {
HILOG_ERROR("observer is null");
}
}
}
void EnableAbilityListsObserverImpl::SubscribeObserver(napi_env env, napi_value observer)
{
HILOG_DEBUG();
@ -955,6 +988,28 @@ void EnableAbilityListsObserverImpl::SubscribeObserver(napi_env env, napi_value
HILOG_DEBUG("observer size%{public}zu", enableAbilityListsObservers_.size());
}
void EnableAbilityListsObserverImpl::SubscribeInstallObserver(napi_env env, napi_value observer)
{
HILOG_DEBUG();
std::lock_guard<std::mutex> lock(mutex_);
for (auto iter = installAbilityListsObservers_.begin(); iter != installAbilityListsObservers_.end();) {
if (CheckObserverEqual(env, observer, (*iter)->env_, (*iter)->callback_)) {
HILOG_DEBUG("Observer exist");
return;
} else {
iter++;
}
}
napi_ref callback = nullptr;
napi_create_reference(env, observer, 1, &callback);
std::shared_ptr<EnableAbilityListsObserver> observerPtr =
std::make_shared<EnableAbilityListsObserver>(env, callback);
installAbilityListsObservers_.emplace_back(observerPtr);
HILOG_DEBUG("observer size%{public}zu", installAbilityListsObservers_.size());
}
void EnableAbilityListsObserverImpl::UnsubscribeObserver(napi_env env, napi_value observer)
{
HILOG_DEBUG();
@ -974,4 +1029,25 @@ void EnableAbilityListsObserverImpl::UnsubscribeObservers()
HILOG_DEBUG();
std::lock_guard<std::mutex> lock(mutex_);
enableAbilityListsObservers_.clear();
}
}
void EnableAbilityListsObserverImpl::UnsubscribeInstallObserver(napi_env env, napi_value observer)
{
HILOG_DEBUG();
std::lock_guard<std::mutex> lock(mutex_);
for (auto iter = installAbilityListsObservers_.begin(); iter != installAbilityListsObservers_.end();) {
if (CheckObserverEqual(env, observer, (*iter)->env_, (*iter)->callback_)) {
installAbilityListsObservers_.erase(iter);
return;
} else {
iter++;
}
}
}
void EnableAbilityListsObserverImpl::UnsubscribeInstallObservers()
{
HILOG_DEBUG();
std::lock_guard<std::mutex> lock(mutex_);
installAbilityListsObservers_.clear();
}

View File

@ -249,6 +249,7 @@ public:
void Init();
void UpdateEnableAbilityListsState();
void UpdateInstallAbilityListsState();
void AddConfigCallback(const sptr<IAccessibleAbilityManagerConfigObserver>& callback);
void RemoveConfigCallback(const wptr<IRemoteObject>& callback);

View File

@ -185,6 +185,16 @@ void AccessibilityAccountData::UpdateEnableAbilityListsState()
}
}
void AccessibilityAccountData::UpdateInstallAbilityListsState()
{
HILOG_DEBUG("observer's size is %{public}zu", enableAbilityListsObservers_.size());
for (auto &observer : enableAbilityListsObservers_) {
if (observer) {
observer->OnAccessibilityInstallAbilityListsChanged();
}
}
}
void AccessibilityAccountData::AddAccessibilityWindowConnection(
const int32_t windowId, const sptr<AccessibilityWindowConnection>& interactionConnection)
{
@ -253,6 +263,7 @@ void AccessibilityAccountData::AddInstalledAbility(AccessibilityAbilityInfo& abi
}
}
installedAbilities_.push_back(abilityInfo);
UpdateInstallAbilityListsState();
HILOG_DEBUG("push back installed ability successfully and installedAbilities_'s size is %{public}zu",
installedAbilities_.size());
}
@ -265,9 +276,11 @@ void AccessibilityAccountData::RemoveInstalledAbility(const std::string &bundleN
HILOG_DEBUG("Removed %{public}s from InstalledAbility: ", bundleName.c_str());
if (!config_) {
it = installedAbilities_.erase(it);
UpdateInstallAbilityListsState();
continue;
}
it = installedAbilities_.erase(it);
UpdateInstallAbilityListsState();
} else {
++it;
}

View File

@ -57,6 +57,7 @@ public:
MOCK_METHOD4(OnRemoteRequest, int(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option));
MOCK_METHOD0(OnAccessibilityEnableAbilityListsChanged, void());
MOCK_METHOD0(OnAccessibilityInstallAbilityListsChanged, void());
};