/* * Copyright (c) 2021-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 "form_ams_helper.h" #include "ability_manager_interface.h" #include "app_mgr_client.h" #include "form_resource_observer.h" #include "fms_log_wrapper.h" #include "form_mgr_errors.h" #include "form_serial_queue.h" #include "if_system_ability_manager.h" #include "in_process_call_wrapper.h" #include "ipc_skeleton.h" #include "iservice_registry.h" #include "system_ability_definition.h" namespace OHOS { namespace AppExecFwk { FormAmsHelper::FormAmsHelper() {} FormAmsHelper::~FormAmsHelper() {} /** * @brief acquire a form ability manager, if it not existed, * @return returns the ability manager ipc object, or nullptr for failed. */ sptr FormAmsHelper::GetAbilityManager() { if (abilityManager_ == nullptr) { sptr systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (systemManager == nullptr) { HILOG_ERROR("get registry failed"); return nullptr; } sptr remoteObject = systemManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID); if (remoteObject == nullptr) { HILOG_ERROR("connect AbilityMgrService failed"); return nullptr; } HILOG_DEBUG("connect AbilityMgrService success"); abilityManager_ = iface_cast(remoteObject); } return abilityManager_; } /** * @brief ConnectAbility, connect session with service ability. * @param want Special want for service type's ability. * @param connect Callback used to notify caller the result of connecting or disconnecting. * @return Returns ERR_OK on success, others on failure. */ ErrCode FormAmsHelper::ConnectServiceAbility( const Want &want, const sptr &connect) { HILOG_DEBUG("connect service ability"); sptr ams = GetAbilityManager(); if (ams == nullptr) { HILOG_ERROR("ability service not connect"); return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED; } return IN_PROCESS_CALL(ams->ConnectAbility(want, connect, nullptr)); } /** * @brief Disconnect ability, disconnect session with service ability. * @param connect Callback used to notify caller the result of connecting or disconnecting. * @return Returns ERR_OK on success, others on failure. */ ErrCode FormAmsHelper::DisconnectServiceAbility(const sptr &connect) { HILOG_INFO("call"); sptr ams = GetAbilityManager(); if (ams == nullptr) { HILOG_ERROR("ability service not connect"); return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED; } return IN_PROCESS_CALL(ams->DisconnectAbility(connect)); } /** * @brief Disconnect ability delay, disconnect session with service ability. * @param connect Callback used to notify caller the result of connecting or disconnecting. * @param delayTime Specifying time to delay, default is FORM_DISCONNECT_DELAY_TIME. * @return Returns ERR_OK on success, others on failure. */ ErrCode FormAmsHelper::DisconnectServiceAbilityDelay(const sptr &connect, int delayTime) { if (serialQueue_ == nullptr) { HILOG_ERROR("null serialQueue_"); return ERR_INVALID_OPERATION; } auto disConnectAbilityFunc = [connect]() { FormAmsHelper::GetInstance().DisconnectAbilityTask(connect); }; if (!serialQueue_->ScheduleTask(delayTime, disConnectAbilityFunc)) { HILOG_ERROR("fail disconnect ability"); return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED; } return ERR_OK; } /** * @brief StopExtensionAbility with want, send want to ability manager service. * @param want The want of the ability to start. * @return Returns ERR_OK on success, others on failure. */ ErrCode FormAmsHelper::StopExtensionAbility(const Want &want) { HILOG_DEBUG("call"); sptr ams = GetAbilityManager(); if (ams == nullptr) { HILOG_ERROR("StopExtensionAbility ability service not connect"); return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED; } return IN_PROCESS_CALL(ams->StopExtensionAbility(want, nullptr)); } /** * @brief Add the ability manager instance for debug. * @param abilityManager the ability manager ipc object. */ void FormAmsHelper::SetAbilityManager(const sptr &abilityManager) { abilityManager_ = abilityManager; } /** * @brief Disconnect ability task, disconnect session with service ability. * @param want Special want for service type's ability. * @param connect Callback used to notify caller the result of connecting or disconnecting. */ void FormAmsHelper::DisconnectAbilityTask(const sptr &connect) { sptr ams = GetAbilityManager(); if (ams == nullptr) { HILOG_ERROR("ability service not connect"); return; } IN_PROCESS_CALL_WITHOUT_RET(ams->DisconnectAbility(connect)); } ErrCode FormAmsHelper::StartAbility(const Want &want, int32_t userId) { sptr ams = GetAbilityManager(); if (ams == nullptr) { HILOG_ERROR("ability service not connect"); return ERR_APPEXECFWK_FORM_BIND_PROVIDER_FAILED; } return IN_PROCESS_CALL(ams->StartAbility(want, userId)); } void FormAmsHelper::RegisterConfigurationObserver() { HILOG_INFO("begin"); if (configurationObserver != nullptr) { HILOG_WARN("configurationObserver not null"); return; } sptr configurationObserver(new (std::nothrow) FormFwkResourceObserver()); if (configurationObserver == nullptr) { HILOG_ERROR("create configurationObserver failed"); return; } auto appMgrClient = std::make_unique(); if (appMgrClient == nullptr) { HILOG_ERROR("create appMgrClient failed"); return; } appMgrClient->RegisterConfigurationObserver(configurationObserver); HILOG_INFO("end"); } void FormAmsHelper::UnRegisterConfigurationObserver() { HILOG_INFO("begin"); if (configurationObserver == nullptr) { HILOG_WARN("null configurationObserver"); return; } auto appMgrClient = std::make_unique(); if (appMgrClient == nullptr) { HILOG_ERROR("create appMgrClient failed"); return; } appMgrClient->UnregisterConfigurationObserver(configurationObserver); HILOG_INFO("end"); } } // namespace AppExecFwk } // namespace OHOS