1 /*
2  * Copyright (c) 2021-2023 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_system_ability.h"
17 #include "delayed_sp_singleton.h"
18 #include "iremote_object.h"
19 #include "new"
20 #include "system_ability_definition.h"
21 #include "display_log.h"
22 
23 namespace OHOS {
24 namespace DisplayPowerMgr {
25 namespace {
26 REGISTER_SYSTEM_ABILITY_BY_ID(DisplaySystemAbility, DISPLAY_MANAGER_SERVICE_ID, true);
27 }
OnStart()28 void DisplaySystemAbility::OnStart()
29 {
30     DISPLAY_HILOGI(COMP_SVC, "DisplayPowerService On Start.");
31     AddSystemAbilityListener(DISPLAY_MANAGER_SERVICE_SA_ID);
32     // we need to listen data service completed when create dpms service
33     AddSystemAbilityListener(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
34 }
35 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)36 void DisplaySystemAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
37 {
38     if (systemAbilityId == DISPLAY_MANAGER_SERVICE_SA_ID) {
39         DISPLAY_HILOGI(COMP_SVC, "DISPLAY_MANAGER_SERVICE_SA_ID loaded");
40         isDpmsLoaded = true;
41     }
42     if (systemAbilityId == DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID) {
43         DISPLAY_HILOGI(COMP_SVC, "DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID loaded");
44         isDataLoaded = true;
45     }
46     if (isDpmsLoaded && isDataLoaded) { // dpms service and data service both ready
47         DISPLAY_HILOGI(COMP_SVC, "Start DisplayPowerMgrService");
48         auto service = DelayedSpSingleton<DisplayPowerMgrService>::GetInstance();
49         service->Init();
50         if (!Publish(service)) {
51             DISPLAY_HILOGE(COMP_SVC, "Failed to publish service");
52         }
53     }
54 }
55 
OnStop()56 void DisplaySystemAbility::OnStop()
57 {
58     DISPLAY_HILOGW(COMP_SVC, "Stop service");
59     auto service = DelayedSpSingleton<DisplayPowerMgrService>::GetInstance();
60     service->Deinit();
61     RemoveSystemAbilityListener(DISPLAY_MANAGER_SERVICE_SA_ID);
62 }
63 } // OHOS
64 } // DisplayPowerMgr
65