1 /*
2 * Copyright (c) 2021-2024 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(COMMON_EVENT_SERVICE_ID);
34 AddSystemAbilityListener(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
35 }
36
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)37 void DisplaySystemAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
38 {
39 if (systemAbilityId == DISPLAY_MANAGER_SERVICE_SA_ID) {
40 DISPLAY_HILOGI(COMP_SVC, "DISPLAY_MANAGER_SERVICE_SA_ID loaded");
41 if (isReady) {
42 DISPLAY_HILOGI(COMP_SVC, "Onstart is ready, nothing to do");
43 return;
44 }
45 auto service = DelayedSpSingleton<DisplayPowerMgrService>::GetInstance();
46 service->Init();
47 if (!Publish(service)) {
48 DISPLAY_HILOGE(COMP_SVC, "Failed to publish service");
49 }
50 if (commonEventManager_ != nullptr && commonEventManager_->CheckIfSettingsDataReady() &&
51 commonEventManager_->SetKvDataReady()) {
52 commonEventManager_->HandleBootBrightness();
53 }
54 isReady = true;
55 }
56 if (systemAbilityId == COMMON_EVENT_SERVICE_ID) {
57 DISPLAY_HILOGI(COMP_SVC, "COMMON_EVENT_SERVICE_ID loaded");
58 OHOS::EventFwk::MatchingSkills matchingSkills;
59 matchingSkills.AddEvent("usual.event.DATA_SHARE_READY");
60 OHOS::EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
61 if (commonEventManager_ == nullptr) {
62 commonEventManager_ = std::make_shared<DisplayCommonEventManager>(subscribeInfo);
63 }
64 if (commonEventManager_ == nullptr ||
65 !OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(commonEventManager_)) {
66 DISPLAY_HILOGI(COMP_SVC, "subscribe common event failed");
67 }
68 }
69 if (systemAbilityId == DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID) {
70 DISPLAY_HILOGI(COMP_SVC, "DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID loaded");
71 if (commonEventManager_ != nullptr && commonEventManager_->CheckIfSettingsDataReady() &&
72 commonEventManager_->SetKvDataReady()) {
73 commonEventManager_->HandleBootBrightness();
74 }
75 }
76 }
77
OnStop()78 void DisplaySystemAbility::OnStop()
79 {
80 DISPLAY_HILOGW(COMP_SVC, "Stop service");
81 auto service = DelayedSpSingleton<DisplayPowerMgrService>::GetInstance();
82 service->Deinit();
83 isReady = false;
84 RemoveSystemAbilityListener(DISPLAY_MANAGER_SERVICE_SA_ID);
85 }
86 } // OHOS
87 } // DisplayPowerMgr
88