1 /*
2  * Copyright (c) 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 #ifdef RSS_DEVICE_STANDBY_ENABLE
17 #include "device_standby_plugin.h"
18 #include "standby_service_client.h"
19 #include "standby_res_data.h"
20 #include "standby_service_log.h"
21 #include "config_info.h"
22 #include "plugin_mgr.h"
23 #include "res_type.h"
24 
25 namespace OHOS {
26 namespace ResourceSchedule {
27 using namespace ResType;
28 namespace {
29     const std::string LIB_NAME = "libdevice_standby_plugin.z.so";
30 }
IMPLEMENT_SINGLE_INSTANCE(DeviceStandbyPlugin)31 IMPLEMENT_SINGLE_INSTANCE(DeviceStandbyPlugin)
32 
33 void DeviceStandbyPlugin::Init()
34 {
35     resTypes_.insert(RES_TYPE_APP_INSTALL_UNINSTALL);
36     resTypes_.insert(RES_TYPE_SCREEN_STATUS);
37     resTypes_.insert(RES_TYPE_TIME_CHANGED);
38     resTypes_.insert(RES_TYPE_NITZ_TIME_CHANGED);
39     resTypes_.insert(RES_TYPE_TIMEZONE_CHANGED);
40     resTypes_.insert(RES_TYPE_NITZ_TIMEZONE_CHANGED);
41     resTypes_.insert(RES_TYPE_CHARGING_DISCHARGING);
42     resTypes_.insert(RES_TYPE_USB_DEVICE);
43     resTypes_.insert(RES_TYPE_CALL_STATE_CHANGED);
44     resTypes_.insert(RES_TYPE_WIFI_P2P_STATE_CHANGED);
45     resTypes_.insert(RES_TYPE_EFFICIENCY_RESOURCES_STATE_CHANGED);
46     resTypes_.insert(RES_TYPE_POWER_MODE_CHANGED);
47 
48     for (auto resType : resTypes_) {
49         PluginMgr::GetInstance().SubscribeResource(LIB_NAME, resType);
50     }
51     STANDBYSERVICE_LOGI("DevicesStandbyPlugin::Init success");
52 }
53 
Disable()54 void DeviceStandbyPlugin::Disable()
55 {
56     for (auto resType : resTypes_) {
57         PluginMgr::GetInstance().UnSubscribeResource(LIB_NAME, resType);
58     }
59     resTypes_.clear();
60     STANDBYSERVICE_LOGI("DevicesStandbyPlugin::Disable success");
61 }
62 
DispatchResource(const std::shared_ptr<ResData> & data)63 void DeviceStandbyPlugin::DispatchResource(const std::shared_ptr<ResData>& data)
64 {
65     if (!data) {
66         STANDBYSERVICE_LOGW("DeviceStandbyPlugin::DispatchResource data is null");
67         return;
68     }
69 
70     STANDBYSERVICE_LOGI(
71         "DeviceStandbyPlugin::DispatchResource type=%{public}u value=%{public}lld",
72         data->resType, (long long)(data->value));
73 
74     DevStandbyMgr::StandbyServiceClient::GetInstance().HandleEvent(
75         std::make_shared<DevStandbyMgr::ResData>(data->resType, data->value, data->payload));
76 }
77 
OnPluginInit(std::string & libName)78 extern "C" bool OnPluginInit(std::string& libName)
79 {
80     if (libName != LIB_NAME) {
81         STANDBYSERVICE_LOGI("DeviceStandbyPlugin::OnPluginInit lib name is not match");
82         return false;
83     }
84     DeviceStandbyPlugin::GetInstance().Init();
85     STANDBYSERVICE_LOGI("DeviceStandbyPlugin::OnPluginInit success.");
86     return true;
87 }
88 
OnPluginDisable()89 extern "C" void OnPluginDisable()
90 {
91     DeviceStandbyPlugin::GetInstance().Disable();
92     STANDBYSERVICE_LOGI("DeviceStandbyPlugin::OnPluginDisable success.");
93 }
94 
OnDispatchResource(const std::shared_ptr<ResData> & data)95 extern "C" void OnDispatchResource(const std::shared_ptr<ResData>& data)
96 {
97     DeviceStandbyPlugin::GetInstance().DispatchResource(data);
98     STANDBYSERVICE_LOGD("DeviceStandbyPlugin::OnDispatchResource success.");
99 }
100 } // namespace ResourceSchedule
101 } // namespace OHOS
102 #endif