/* * Copyright (c) 2021-2022 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 "display_power_mgr_client.h" #include #include #include #include "new" #include "refbase.h" #include "iremote_broker.h" #include "iremote_object.h" #include "display_log.h" #include "display_common.h" #include "display_power_info.h" #include "idisplay_power_callback.h" #include "idisplay_power_mgr.h" #include "power_state_machine_info.h" namespace OHOS { namespace DisplayPowerMgr { DisplayPowerMgrClient::DisplayPowerMgrClient() = default; DisplayPowerMgrClient::~DisplayPowerMgrClient() = default; sptr DisplayPowerMgrClient::GetProxy() { std::lock_guard lock(mutex_); if (proxy_ != nullptr) { return proxy_; } sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (sam == nullptr) { DISPLAY_HILOGE(COMP_FWK, "Failed to get system ability manager"); lastError_ = DisplayErrors::ERR_CONNECTION_FAIL; return nullptr; } sptr obj = sam->CheckSystemAbility(DISPLAY_MANAGER_SERVICE_ID); if (obj == nullptr) { lastError_ = DisplayErrors::ERR_CONNECTION_FAIL; DISPLAY_HILOGE(COMP_FWK, "Failed to get display manager service"); return nullptr; } sptr dr = new DisplayDeathRecipient(*this); if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) { DISPLAY_HILOGE(COMP_FWK, "Failed to add death recipient"); lastError_ = DisplayErrors::ERR_CONNECTION_FAIL; return nullptr; } proxy_ = iface_cast(obj); deathRecipient_ = dr; DISPLAY_HILOGI(COMP_FWK, "Succeed to connect display manager service"); return proxy_; } void DisplayPowerMgrClient::OnRemoteDied(const wptr& remote) { if (remote == nullptr) { DISPLAY_HILOGE(COMP_FWK, "remote is nullptr"); return; } std::lock_guard lock(mutex_); RETURN_IF(proxy_ == nullptr); auto serviceRemote = proxy_->AsObject(); if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) { serviceRemote->RemoveDeathRecipient(deathRecipient_); proxy_ = nullptr; } } bool DisplayPowerMgrClient::SetDisplayState(DisplayState state, PowerMgr::StateChangeReason reason, uint32_t id) { auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, false); return proxy->SetDisplayState(id, state, static_cast(reason)); } DisplayState DisplayPowerMgrClient::GetDisplayState(uint32_t id) { auto proxy = GetProxy(); if (proxy == nullptr) { return DisplayState::DISPLAY_UNKNOWN; } return proxy->GetDisplayState(id); } std::vector DisplayPowerMgrClient::GetDisplayIds() { auto proxy = GetProxy(); if (proxy == nullptr) { return {}; } return proxy->GetDisplayIds(); } int32_t DisplayPowerMgrClient::GetMainDisplayId() { auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, INVALID_DISPLAY_ID); return static_cast(proxy->GetMainDisplayId()); } bool DisplayPowerMgrClient::SetBrightness(uint32_t value, uint32_t displayId) { auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, false); return proxy->SetBrightness(value, displayId); } bool DisplayPowerMgrClient::DiscountBrightness(double discount, uint32_t displayId) { auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, false); return proxy->DiscountBrightness(discount, displayId); } bool DisplayPowerMgrClient::OverrideBrightness(uint32_t value, uint32_t displayId) { auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, false); return proxy->OverrideBrightness(value, displayId); } bool DisplayPowerMgrClient::RestoreBrightness(uint32_t displayId) { auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, false); return proxy->RestoreBrightness(displayId); } uint32_t DisplayPowerMgrClient::GetBrightness(uint32_t displayId) { auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, BRIGHTNESS_OFF); return proxy->GetBrightness(displayId); } uint32_t DisplayPowerMgrClient::GetDefaultBrightness() { auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, BRIGHTNESS_DEFAULT); return proxy->GetDefaultBrightness(); } uint32_t DisplayPowerMgrClient::GetMaxBrightness() { auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, BRIGHTNESS_MAX); return proxy->GetMaxBrightness(); } uint32_t DisplayPowerMgrClient::GetMinBrightness() { auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, BRIGHTNESS_MIN); return proxy->GetMinBrightness(); } bool DisplayPowerMgrClient::AdjustBrightness(uint32_t value, uint32_t duration, uint32_t id) { auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, false); return proxy->AdjustBrightness(id, value, duration); } bool DisplayPowerMgrClient::AutoAdjustBrightness(bool enable) { auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, false); return proxy->AutoAdjustBrightness(enable); } bool DisplayPowerMgrClient::IsAutoAdjustBrightness() { auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, false); return proxy->IsAutoAdjustBrightness(); } bool DisplayPowerMgrClient::RegisterCallback(sptr callback) { if (callback == nullptr) { DISPLAY_HILOGE(COMP_FWK, "callback is nullptr"); return false; } auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, false); return proxy->RegisterCallback(callback); } bool DisplayPowerMgrClient::BoostBrightness(int32_t timeoutMs, uint32_t displayId) { auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, false); return proxy->BoostBrightness(timeoutMs, displayId); } bool DisplayPowerMgrClient::CancelBoostBrightness(uint32_t displayId) { auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, false); return proxy->CancelBoostBrightness(displayId); } uint32_t DisplayPowerMgrClient::GetDeviceBrightness(uint32_t displayId) { auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, 0); return proxy->GetDeviceBrightness(displayId); } DisplayErrors DisplayPowerMgrClient::GetError() { if (lastError_ != DisplayErrors::ERR_OK) { DisplayErrors tmpError = lastError_; lastError_ = DisplayErrors::ERR_OK; return tmpError; } auto proxy = GetProxy(); RETURN_IF_WITH_RET(proxy == nullptr, DisplayErrors::ERR_CONNECTION_FAIL); return proxy->GetError(); } } // namespace DisplayPowerMgr } // namespace OHOS