1cb69b360Sopenharmony_ci/*
2cb69b360Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3cb69b360Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4cb69b360Sopenharmony_ci * you may not use this file except in compliance with the License.
5cb69b360Sopenharmony_ci * You may obtain a copy of the License at
6cb69b360Sopenharmony_ci *
7cb69b360Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8cb69b360Sopenharmony_ci *
9cb69b360Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10cb69b360Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11cb69b360Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cb69b360Sopenharmony_ci * See the License for the specific language governing permissions and
13cb69b360Sopenharmony_ci * limitations under the License.
14cb69b360Sopenharmony_ci */
15cb69b360Sopenharmony_ci
16cb69b360Sopenharmony_ci#include "screen_saver_feature.h"
17cb69b360Sopenharmony_ci
18cb69b360Sopenharmony_ci#include <feature.h>
19cb69b360Sopenharmony_ci#include <ohos_init.h>
20cb69b360Sopenharmony_ci#include <samgr_lite.h>
21cb69b360Sopenharmony_ci
22cb69b360Sopenharmony_ci#include "hilog_wrapper.h"
23cb69b360Sopenharmony_ci#include "power_mgr.h"
24cb69b360Sopenharmony_ci#include "screen_saver_intf_define.h"
25cb69b360Sopenharmony_ci#include "screen_saver_mgr.h"
26cb69b360Sopenharmony_ci
27cb69b360Sopenharmony_ciconst char *GetScreenSaverFeatureName(Feature *feature)
28cb69b360Sopenharmony_ci{
29cb69b360Sopenharmony_ci    (void)feature;
30cb69b360Sopenharmony_ci    return POWER_SCREEN_SAVER_FEATURE;
31cb69b360Sopenharmony_ci}
32cb69b360Sopenharmony_ci
33cb69b360Sopenharmony_civoid OnScreenSaverFeatureInitialize(Feature *feature, Service *parent, Identity identity)
34cb69b360Sopenharmony_ci{
35cb69b360Sopenharmony_ci    (void)parent;
36cb69b360Sopenharmony_ci    (void)identity;
37cb69b360Sopenharmony_ci    if (feature == NULL) {
38cb69b360Sopenharmony_ci        POWER_HILOGE("Invalid feature");
39cb69b360Sopenharmony_ci        return;
40cb69b360Sopenharmony_ci    }
41cb69b360Sopenharmony_ci
42cb69b360Sopenharmony_ci    ScreenSaverFeature *f = (ScreenSaverFeature *)feature;
43cb69b360Sopenharmony_ci    f->identity = identity;
44cb69b360Sopenharmony_ci    ScreenSaverMgrInit();
45cb69b360Sopenharmony_ci    POWER_HILOGI("Init screen saver feature done");
46cb69b360Sopenharmony_ci}
47cb69b360Sopenharmony_ci
48cb69b360Sopenharmony_civoid OnScreenSaverFeatureStop(Feature *feature, Identity identity)
49cb69b360Sopenharmony_ci{
50cb69b360Sopenharmony_ci    (void)feature;
51cb69b360Sopenharmony_ci    (void)identity;
52cb69b360Sopenharmony_ci    ScreenSaverFeature *f = GetScreenSaverFeatureImpl();
53cb69b360Sopenharmony_ci    if (f != NULL) {
54cb69b360Sopenharmony_ci        f->identity.queueId = NULL;
55cb69b360Sopenharmony_ci        f->identity.featureId = -1;
56cb69b360Sopenharmony_ci        f->identity.serviceId = -1;
57cb69b360Sopenharmony_ci    }
58cb69b360Sopenharmony_ci}
59cb69b360Sopenharmony_ci
60cb69b360Sopenharmony_ciBOOL OnScreenSaverFeatureMessage(Feature *feature, Request *request)
61cb69b360Sopenharmony_ci{
62cb69b360Sopenharmony_ci    return ((feature != NULL) && (request != NULL)) ? TRUE : FALSE;
63cb69b360Sopenharmony_ci}
64cb69b360Sopenharmony_ci
65cb69b360Sopenharmony_ciint32_t OnSetScreenSaverState(IUnknown *iUnknown, BOOL enable)
66cb69b360Sopenharmony_ci{
67cb69b360Sopenharmony_ci    (void)iUnknown;
68cb69b360Sopenharmony_ci    return ScreenSaverMgrSetState(enable);
69cb69b360Sopenharmony_ci}
70cb69b360Sopenharmony_ci
71cb69b360Sopenharmony_cistatic void Init(void)
72cb69b360Sopenharmony_ci{
73cb69b360Sopenharmony_ci    SamgrLite *sam = SAMGR_GetInstance();
74cb69b360Sopenharmony_ci    if (sam == NULL) {
75cb69b360Sopenharmony_ci        POWER_HILOGE("Failed to get samgr");
76cb69b360Sopenharmony_ci        return;
77cb69b360Sopenharmony_ci    }
78cb69b360Sopenharmony_ci
79cb69b360Sopenharmony_ci    ScreenSaverFeature *feature = GetScreenSaverFeatureImpl();
80cb69b360Sopenharmony_ci    if (feature == NULL) {
81cb69b360Sopenharmony_ci        POWER_HILOGE("Failed to get screen saver feature");
82cb69b360Sopenharmony_ci        return;
83cb69b360Sopenharmony_ci    }
84cb69b360Sopenharmony_ci    BOOL result = sam->RegisterFeature(POWER_MANAGE_SERVICE, (Feature *)feature);
85cb69b360Sopenharmony_ci    if (result == FALSE) {
86cb69b360Sopenharmony_ci        POWER_HILOGE("Failed to register screen saver feature");
87cb69b360Sopenharmony_ci        return;
88cb69b360Sopenharmony_ci    }
89cb69b360Sopenharmony_ci    result = sam->RegisterFeatureApi(POWER_MANAGE_SERVICE, POWER_SCREEN_SAVER_FEATURE, GET_IUNKNOWN(*feature));
90cb69b360Sopenharmony_ci    if (result == FALSE) {
91cb69b360Sopenharmony_ci        POWER_HILOGE("Failed to register screen saver feature api");
92cb69b360Sopenharmony_ci        return;
93cb69b360Sopenharmony_ci    }
94cb69b360Sopenharmony_ci    POWER_HILOGI("Succeed to register screen saver feature");
95cb69b360Sopenharmony_ci}
96cb69b360Sopenharmony_ciSYS_FEATURE_INIT(Init);
97