• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 auto g_service = DelayedSpSingleton<ThermalService>::GetInstance();
34 }
Init()35 bool ScreenStateCollection::Init()
36 {
37     if (!RegisterEvent()) {
38         return false;
39     }
40     return true;
41 }
InitParam(std::string & params)42 bool ScreenStateCollection::InitParam(std::string& params)
43 {
44     THERMAL_HILOGD(COMP_SVC, "Enter");
45     params_ = params;
46     return true;
47 }
48 
GetState()49 std::string ScreenStateCollection::GetState()
50 {
51     THERMAL_HILOGD(COMP_SVC, "screen state = %{public}s", mockState_.c_str());
52     if (!g_service->GetFlag()) {
53         return mockState_;
54     } else {
55         return state_;
56     }
57 }
58 
RegisterEvent()59 bool ScreenStateCollection::RegisterEvent()
60 {
61     THERMAL_HILOGD(COMP_SVC, "Enter");
62     if (g_service == nullptr) return false;
63     auto receiver = g_service->GetStateMachineObj()->GetCommonEventReceiver();
64     if (receiver == nullptr) return false;
65     THERMAL_HILOGI(COMP_SVC, "register screen on event");
66     EventHandle handlerOn = std::bind(&ScreenStateCollection::HandleScreenOnCompleted, this, std::placeholders::_1);
67     bool on = receiver->Start(CommonEventSupport::COMMON_EVENT_SCREEN_ON, handlerOn);
68     if (!on) {
69         THERMAL_HILOGE(COMP_SVC, "fail to COMMON_EVENT_SCREEN_ON");
70         return false;
71     }
72     THERMAL_HILOGI(COMP_SVC, "register screen off event");
73     EventHandle handlerOff = std::bind(&ScreenStateCollection::HandleScreenOffCompleted, this, std::placeholders::_1);
74     bool off = receiver->Start(CommonEventSupport::COMMON_EVENT_SCREEN_OFF, handlerOff);
75     if (!off) {
76         THERMAL_HILOGE(COMP_SVC, "fail to COMMON_EVENT_SCREEN_OFF");
77         return false;
78     }
79     return true;
80 }
81 
HandleScreenOnCompleted(const CommonEventData & data)82 void ScreenStateCollection::HandleScreenOnCompleted(const CommonEventData& data __attribute__((__unused__)))
83 {
84     THERMAL_HILOGD(COMP_SVC, "Enter");
85     state_ = ToString(SCREEN_ON);
86 }
87 
HandleScreenOffCompleted(const CommonEventData & data)88 void ScreenStateCollection::HandleScreenOffCompleted(const CommonEventData& data __attribute__((__unused__)))
89 {
90     THERMAL_HILOGD(COMP_SVC, "Enter");
91     state_ = ToString(SCREEN_OFF);
92 }
93 
SetState()94 void ScreenStateCollection::SetState()
95 {
96 }
97 
DecideState(const std::string & value)98 bool ScreenStateCollection::DecideState(const std::string& value)
99 {
100     auto& powerMgrClient = PowerMgrClient::GetInstance();
101     if ((value == ToString(SCREEN_ON) && powerMgrClient.IsScreenOn()) ||
102         (value == ToString(SCREEN_OFF) && !powerMgrClient.IsScreenOn())) {
103         return true;
104     }
105     return false;
106 }
107 } // namespace PowerMgr
108 } // namespace OHOS
109