1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "display_power_mgr_client.h"
17
18 #include <if_system_ability_manager.h>
19 #include <iservice_registry.h>
20 #include <system_ability_definition.h>
21 #include "new"
22 #include "refbase.h"
23 #include "iremote_broker.h"
24 #include "iremote_object.h"
25 #include "display_log.h"
26 #include "display_common.h"
27 #include "display_power_info.h"
28 #include "idisplay_power_callback.h"
29 #include "idisplay_power_mgr.h"
30 #include "power_state_machine_info.h"
31
32 namespace OHOS {
33 namespace DisplayPowerMgr {
34 DisplayPowerMgrClient::DisplayPowerMgrClient() = default;
35 DisplayPowerMgrClient::~DisplayPowerMgrClient() = default;
36
GetProxy()37 sptr<IDisplayPowerMgr> DisplayPowerMgrClient::GetProxy()
38 {
39 std::lock_guard lock(mutex_);
40 if (proxy_ != nullptr) {
41 return proxy_;
42 }
43
44 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
45 if (sam == nullptr) {
46 DISPLAY_HILOGE(COMP_FWK, "Failed to get system ability manager");
47 lastError_ = DisplayErrors::ERR_CONNECTION_FAIL;
48 return nullptr;
49 }
50 sptr<IRemoteObject> obj = sam->CheckSystemAbility(DISPLAY_MANAGER_SERVICE_ID);
51 if (obj == nullptr) {
52 lastError_ = DisplayErrors::ERR_CONNECTION_FAIL;
53 DISPLAY_HILOGE(COMP_FWK, "Failed to get display manager service");
54 return nullptr;
55 }
56 sptr<IRemoteObject::DeathRecipient> dr = new DisplayDeathRecipient(*this);
57 if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) {
58 DISPLAY_HILOGE(COMP_FWK, "Failed to add death recipient");
59 lastError_ = DisplayErrors::ERR_CONNECTION_FAIL;
60 return nullptr;
61 }
62
63 proxy_ = iface_cast<IDisplayPowerMgr>(obj);
64 deathRecipient_ = dr;
65 DISPLAY_HILOGI(COMP_FWK, "Succeed to connect display manager service");
66 return proxy_;
67 }
68
OnRemoteDied(const wptr<IRemoteObject> & remote)69 void DisplayPowerMgrClient::OnRemoteDied(const wptr<IRemoteObject>& remote)
70 {
71 if (remote == nullptr) {
72 DISPLAY_HILOGE(COMP_FWK, "remote is nullptr");
73 return;
74 }
75
76 std::lock_guard lock(mutex_);
77 RETURN_IF(proxy_ == nullptr);
78
79 auto serviceRemote = proxy_->AsObject();
80 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
81 serviceRemote->RemoveDeathRecipient(deathRecipient_);
82 proxy_ = nullptr;
83 }
84 }
85
SetDisplayState(DisplayState state,PowerMgr::StateChangeReason reason,uint32_t id)86 bool DisplayPowerMgrClient::SetDisplayState(DisplayState state,
87 PowerMgr::StateChangeReason reason, uint32_t id)
88 {
89 auto proxy = GetProxy();
90 RETURN_IF_WITH_RET(proxy == nullptr, false);
91 return proxy->SetDisplayState(id, state, static_cast<uint32_t>(reason));
92 }
93
GetDisplayState(uint32_t id)94 DisplayState DisplayPowerMgrClient::GetDisplayState(uint32_t id)
95 {
96 auto proxy = GetProxy();
97 if (proxy == nullptr) {
98 return DisplayState::DISPLAY_UNKNOWN;
99 }
100 return proxy->GetDisplayState(id);
101 }
102
GetDisplayIds()103 std::vector<uint32_t> DisplayPowerMgrClient::GetDisplayIds()
104 {
105 auto proxy = GetProxy();
106 if (proxy == nullptr) {
107 return {};
108 }
109 return proxy->GetDisplayIds();
110 }
111
GetMainDisplayId()112 int32_t DisplayPowerMgrClient::GetMainDisplayId()
113 {
114 auto proxy = GetProxy();
115 RETURN_IF_WITH_RET(proxy == nullptr, INVALID_DISPLAY_ID);
116 return static_cast<int32_t>(proxy->GetMainDisplayId());
117 }
118
SetBrightness(uint32_t value,uint32_t displayId)119 bool DisplayPowerMgrClient::SetBrightness(uint32_t value, uint32_t displayId)
120 {
121 auto proxy = GetProxy();
122 RETURN_IF_WITH_RET(proxy == nullptr, false);
123 return proxy->SetBrightness(value, displayId);
124 }
125
DiscountBrightness(double discount,uint32_t displayId)126 bool DisplayPowerMgrClient::DiscountBrightness(double discount, uint32_t displayId)
127 {
128 auto proxy = GetProxy();
129 RETURN_IF_WITH_RET(proxy == nullptr, false);
130 return proxy->DiscountBrightness(discount, displayId);
131 }
132
OverrideBrightness(uint32_t value,uint32_t displayId)133 bool DisplayPowerMgrClient::OverrideBrightness(uint32_t value, uint32_t displayId)
134 {
135 auto proxy = GetProxy();
136 RETURN_IF_WITH_RET(proxy == nullptr, false);
137 return proxy->OverrideBrightness(value, displayId);
138 }
139
RestoreBrightness(uint32_t displayId)140 bool DisplayPowerMgrClient::RestoreBrightness(uint32_t displayId)
141 {
142 auto proxy = GetProxy();
143 RETURN_IF_WITH_RET(proxy == nullptr, false);
144 return proxy->RestoreBrightness(displayId);
145 }
146
GetBrightness(uint32_t displayId)147 uint32_t DisplayPowerMgrClient::GetBrightness(uint32_t displayId)
148 {
149 auto proxy = GetProxy();
150 RETURN_IF_WITH_RET(proxy == nullptr, BRIGHTNESS_OFF);
151 return proxy->GetBrightness(displayId);
152 }
153
GetDefaultBrightness()154 uint32_t DisplayPowerMgrClient::GetDefaultBrightness()
155 {
156 auto proxy = GetProxy();
157 RETURN_IF_WITH_RET(proxy == nullptr, BRIGHTNESS_DEFAULT);
158 return proxy->GetDefaultBrightness();
159 }
160
GetMaxBrightness()161 uint32_t DisplayPowerMgrClient::GetMaxBrightness()
162 {
163 auto proxy = GetProxy();
164 RETURN_IF_WITH_RET(proxy == nullptr, BRIGHTNESS_MAX);
165 return proxy->GetMaxBrightness();
166 }
167
GetMinBrightness()168 uint32_t DisplayPowerMgrClient::GetMinBrightness()
169 {
170 auto proxy = GetProxy();
171 RETURN_IF_WITH_RET(proxy == nullptr, BRIGHTNESS_MIN);
172 return proxy->GetMinBrightness();
173 }
174
AdjustBrightness(uint32_t value,uint32_t duration,uint32_t id)175 bool DisplayPowerMgrClient::AdjustBrightness(uint32_t value, uint32_t duration, uint32_t id)
176 {
177 auto proxy = GetProxy();
178 RETURN_IF_WITH_RET(proxy == nullptr, false);
179 return proxy->AdjustBrightness(id, value, duration);
180 }
181
AutoAdjustBrightness(bool enable)182 bool DisplayPowerMgrClient::AutoAdjustBrightness(bool enable)
183 {
184 auto proxy = GetProxy();
185 RETURN_IF_WITH_RET(proxy == nullptr, false);
186 return proxy->AutoAdjustBrightness(enable);
187 }
188
IsAutoAdjustBrightness()189 bool DisplayPowerMgrClient::IsAutoAdjustBrightness()
190 {
191 auto proxy = GetProxy();
192 RETURN_IF_WITH_RET(proxy == nullptr, false);
193 return proxy->IsAutoAdjustBrightness();
194 }
195
RegisterCallback(sptr<IDisplayPowerCallback> callback)196 bool DisplayPowerMgrClient::RegisterCallback(sptr<IDisplayPowerCallback> callback)
197 {
198 if (callback == nullptr) {
199 DISPLAY_HILOGE(COMP_FWK, "callback is nullptr");
200 return false;
201 }
202
203 auto proxy = GetProxy();
204 RETURN_IF_WITH_RET(proxy == nullptr, false);
205 return proxy->RegisterCallback(callback);
206 }
207
BoostBrightness(int32_t timeoutMs,uint32_t displayId)208 bool DisplayPowerMgrClient::BoostBrightness(int32_t timeoutMs, uint32_t displayId)
209 {
210 auto proxy = GetProxy();
211 RETURN_IF_WITH_RET(proxy == nullptr, false);
212 return proxy->BoostBrightness(timeoutMs, displayId);
213 }
214
CancelBoostBrightness(uint32_t displayId)215 bool DisplayPowerMgrClient::CancelBoostBrightness(uint32_t displayId)
216 {
217 auto proxy = GetProxy();
218 RETURN_IF_WITH_RET(proxy == nullptr, false);
219 return proxy->CancelBoostBrightness(displayId);
220 }
221
GetDeviceBrightness(uint32_t displayId)222 uint32_t DisplayPowerMgrClient::GetDeviceBrightness(uint32_t displayId)
223 {
224 auto proxy = GetProxy();
225 RETURN_IF_WITH_RET(proxy == nullptr, 0);
226 return proxy->GetDeviceBrightness(displayId);
227 }
228
GetError()229 DisplayErrors DisplayPowerMgrClient::GetError()
230 {
231 if (lastError_ != DisplayErrors::ERR_OK) {
232 DisplayErrors tmpError = lastError_;
233 lastError_ = DisplayErrors::ERR_OK;
234 return tmpError;
235 }
236 auto proxy = GetProxy();
237 RETURN_IF_WITH_RET(proxy == nullptr, DisplayErrors::ERR_CONNECTION_FAIL);
238 return proxy->GetError();
239 }
240 } // namespace DisplayPowerMgr
241 } // namespace OHOS
242