1 /*
2 * Copyright (c) 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_common_event_mgr.h"
17
18 #include <string>
19
20 #include "display_system_ability.h"
21 #include "display_power_mgr_service.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24
25
26 namespace OHOS {
27 namespace DisplayPowerMgr {
28 namespace {
29 constexpr const char *SETTING_URI_PROXY =
30 "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true";
31 constexpr const char *SETTINGS_DATA_EXT_URI = "datashare_ext";
32 }
33 using CommonEventSubscriber = OHOS::EventFwk::CommonEventSubscriber;
34 using CommonEventSubscribeInfo = OHOS::EventFwk::CommonEventSubscribeInfo;
35 using CommonEventData = OHOS::EventFwk::CommonEventData;
36
DisplayCommonEventManager(const CommonEventSubscribeInfo & subscribeInfo)37 DisplayCommonEventManager::DisplayCommonEventManager(const CommonEventSubscribeInfo &subscribeInfo)
38 : CommonEventSubscriber(subscribeInfo)
39 {
40 }
41
OnReceiveEvent(const CommonEventData & data)42 void DisplayCommonEventManager::OnReceiveEvent(const CommonEventData &data)
43 {
44 std::string action = data.GetWant().GetAction();
45 if (action == "usual.event.DATA_SHARE_READY") {
46 DISPLAY_HILOGI(COMP_SVC, "on receive data_share ready.");
47 if (GetDpmsRemoteObj() && SetKvDataReady()) {
48 HandleBootBrightness();
49 }
50 }
51 }
52
CheckIfSettingsDataReady()53 bool DisplayCommonEventManager::CheckIfSettingsDataReady()
54 {
55 if (isDataShareReady_) {
56 return true;
57 }
58 auto remoteObj = GetDpmsRemoteObj();
59 if (remoteObj == nullptr) {
60 DISPLAY_HILOGE(COMP_SVC, "remote obj is null");
61 return false;
62 }
63 return CreateDataShareHelper(remoteObj);
64 }
65
GetDpmsRemoteObj()66 sptr<IRemoteObject> DisplayCommonEventManager::GetDpmsRemoteObj()
67 {
68 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
69 if (sam == nullptr) {
70 DISPLAY_HILOGE(COMP_SVC, "sa manager is null");
71 return nullptr;
72 }
73 return sam->CheckSystemAbility(DISPLAY_MANAGER_SERVICE_ID);
74 }
75
CreateDataShareHelper(sptr<IRemoteObject> remoteObj)76 bool DisplayCommonEventManager::CreateDataShareHelper(sptr<IRemoteObject> remoteObj)
77 {
78 std::pair<int, std::shared_ptr<DataShare::DataShareHelper>> ret =
79 DataShare::DataShareHelper::Create(remoteObj, SETTING_URI_PROXY, SETTINGS_DATA_EXT_URI);
80 DISPLAY_HILOGI(COMP_SVC, "create data_share helper, ret=%{public}d", ret.first);
81 if (ret.first == DataShare::E_OK) {
82 DISPLAY_HILOGI(COMP_SVC, "create data_share helper success");
83 auto helper = ret.second;
84 if (helper != nullptr) {
85 bool releaseRet = helper->Release();
86 DISPLAY_HILOGI(COMP_SVC, "release data_share helper, releaseRet=%{public}d", releaseRet);
87 }
88 isDataShareReady_ = true;
89 return true;
90 } else {
91 DISPLAY_HILOGE(COMP_SVC, "create data_share helper failed");
92 isDataShareReady_ = false;
93 return false;
94 }
95 }
96
SetKvDataReady()97 bool DisplayCommonEventManager::SetKvDataReady()
98 {
99 std::lock_guard<std::mutex> lock(lock_);
100 if (isKvDataReady_) {
101 return false;
102 }
103 isKvDataReady_ = true;
104 return true;
105 }
106
HandleBootBrightness()107 void DisplayCommonEventManager::HandleBootBrightness()
108 {
109 DISPLAY_HILOGI(COMP_SVC, "set boot brightness");
110 auto service = DelayedSpSingleton<DisplayPowerMgrService>::GetInstance();
111 service->HandleBootBrightness();
112 }
113 } // namespace DisplayPowerMgr
114 } // namespace OHOS
115