/* * Copyright (C) 2021 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 "telephony_observer_client.h" #include "if_system_ability_manager.h" #include "iservice_registry.h" #include "state_registry_errors.h" #include "system_ability_definition.h" #include "telephony_log_wrapper.h" #include "telephony_state_registry_proxy.h" namespace OHOS { namespace Telephony { TelephonyObserverClient::TelephonyObserverClient() = default; TelephonyObserverClient::~TelephonyObserverClient() = default; sptr TelephonyObserverClient::GetProxy() { std::lock_guard lock(mutexProxy_); if (proxy_ != nullptr) { return proxy_; } sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (sam == nullptr) { TELEPHONY_LOGE("Failed to get system ability manager"); return nullptr; } sptr obj = sam->CheckSystemAbility(TELEPHONY_STATE_REGISTRY_SYS_ABILITY_ID); if (obj == nullptr) { TELEPHONY_LOGE("Failed to get state registry service"); return nullptr; } std::unique_ptr recipient = std::make_unique(*this); if (recipient == nullptr) { TELEPHONY_LOGE("recipient is null"); return nullptr; } sptr dr(recipient.release()); if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) { TELEPHONY_LOGE("Failed to add death recipient"); return nullptr; } proxy_ = iface_cast(obj); deathRecipient_ = dr; TELEPHONY_LOGI("Succeed to connect state registry service %{public}d", proxy_ == nullptr); return proxy_; } void TelephonyObserverClient::OnRemoteDied(const wptr &remote) { if (remote == nullptr) { TELEPHONY_LOGE("OnRemoteDied failed, remote is nullptr"); return; } std::lock_guard lock(mutexProxy_); if (proxy_ == nullptr) { TELEPHONY_LOGE("OnRemoteDied proxy_ is nullptr"); return; } auto serviceRemote = proxy_->AsObject(); if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) { serviceRemote->RemoveDeathRecipient(deathRecipient_); proxy_ = nullptr; TELEPHONY_LOGE("on remote died"); } } int32_t TelephonyObserverClient::AddStateObserver(const sptr &telephonyObserver, int32_t slotId, uint32_t mask, bool isUpdate) { auto proxy = GetProxy(); if (proxy == nullptr) { TELEPHONY_LOGE("proxy is null!"); return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL; } return proxy->RegisterStateChange(telephonyObserver, slotId, mask, isUpdate); } int32_t TelephonyObserverClient::RemoveStateObserver(int32_t slotId, uint32_t mask) { auto proxy = GetProxy(); if (proxy == nullptr) { TELEPHONY_LOGE("proxy is null!"); return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL; } return proxy->UnregisterStateChange(slotId, mask); } } }