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 "screen_state_collection.h"
17
18 #include "common_event_support.h"
19 #include "file_operation.h"
20 #include "power_mgr_client.h"
21 #include "securec.h"
22 #include "string_ex.h"
23 #include "string_operation.h"
24 #include "thermal_service.h"
25 #include "thermal_common.h"
26
27 using namespace OHOS::EventFwk;
28 namespace OHOS {
29 namespace PowerMgr {
30 namespace {
31 const uint32_t SCREEN_ON = 1;
32 const uint32_t SCREEN_OFF = 0;
33 }
Init()34 bool ScreenStateCollection::Init()
35 {
36 if (!RegisterEvent()) {
37 return false;
38 }
39 return true;
40 }
InitParam(std::string & params)41 bool ScreenStateCollection::InitParam(std::string& params)
42 {
43 THERMAL_HILOGD(COMP_SVC, "Enter");
44 params_ = params;
45 return true;
46 }
47
GetState()48 std::string ScreenStateCollection::GetState()
49 {
50 THERMAL_HILOGD(COMP_SVC, "screen state = %{public}s", mockState_.c_str());
51 auto tms = ThermalService::GetInstance();
52 if (!tms->GetSimulationXml()) {
53 return mockState_;
54 } else {
55 return state_;
56 }
57 }
58
RegisterEvent()59 bool ScreenStateCollection::RegisterEvent()
60 {
61 THERMAL_HILOGD(COMP_SVC, "Enter");
62 auto tms = ThermalService::GetInstance();
63 if (tms == nullptr) {
64 return false;
65 }
66 auto receiver = tms->GetStateMachineObj()->GetCommonEventReceiver();
67 if (receiver == nullptr) {
68 return false;
69 }
70 EventHandle handlerOn = [this](const CommonEventData& data) { this->HandleScreenOnCompleted(data); };
71 receiver->AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_ON, handlerOn);
72 EventHandle handlerOff = [this](const CommonEventData& data) { this->HandleScreenOffCompleted(data); };
73 receiver->AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF, handlerOff);
74 return true;
75 }
76
HandleScreenOnCompleted(const CommonEventData & data)77 void ScreenStateCollection::HandleScreenOnCompleted(const CommonEventData& data __attribute__((__unused__)))
78 {
79 THERMAL_HILOGD(COMP_SVC, "received screen on event");
80 state_ = ToString(SCREEN_ON);
81 }
82
HandleScreenOffCompleted(const CommonEventData & data)83 void ScreenStateCollection::HandleScreenOffCompleted(const CommonEventData& data __attribute__((__unused__)))
84 {
85 THERMAL_HILOGD(COMP_SVC, "received screen off event");
86 state_ = ToString(SCREEN_OFF);
87 }
88
SetState(const std::string & stateValue)89 void ScreenStateCollection::SetState(const std::string& stateValue)
90 {
91 }
92
DecideState(const std::string & value)93 bool ScreenStateCollection::DecideState(const std::string& value)
94 {
95 auto& powerMgrClient = PowerMgrClient::GetInstance();
96 if ((value == ToString(SCREEN_ON) && powerMgrClient.IsScreenOn(false)) ||
97 (value == ToString(SCREEN_OFF) && !powerMgrClient.IsScreenOn(false))) {
98 return true;
99 }
100 return false;
101 }
102 } // namespace PowerMgr
103 } // namespace OHOS
104