• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "setting_helper.h"
17 
18 #include "power_log.h"
19 #include <cinttypes>
20 #include <system_ability_definition.h>
21 
22 namespace OHOS {
23 namespace PowerMgr {
IsDisplayOffTimeSettingValid()24 bool SettingHelper::IsDisplayOffTimeSettingValid()
25 {
26     return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(SETTING_DISPLAY_OFF_TIME_KEY);
27 }
28 
GetSettingDisplayOffTime(int64_t defaultVal)29 int64_t SettingHelper::GetSettingDisplayOffTime(int64_t defaultVal)
30 {
31     SettingProvider& provider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
32     int64_t value = defaultVal;
33     ErrCode ret = provider.GetLongValue(SETTING_DISPLAY_OFF_TIME_KEY, value);
34     if (ret != ERR_OK) {
35         POWER_HILOGW(COMP_UTILS, "get setting display off time failed, ret=%{public}d", ret);
36     }
37     return value;
38 }
39 
SetSettingDisplayOffTime(int64_t time)40 void SettingHelper::SetSettingDisplayOffTime(int64_t time)
41 {
42     SettingProvider& provider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
43     ErrCode ret = provider.PutLongValue(SETTING_DISPLAY_OFF_TIME_KEY, time);
44     if (ret != ERR_OK) {
45         POWER_HILOGW(
46             COMP_UTILS, "set setting display off time failed, time=%{public}" PRId64 ", ret=%{public}d", time, ret);
47     }
48 }
49 
RegisterSettingDisplayOffTimeObserver(SettingObserver::UpdateFunc & func)50 sptr<SettingObserver> SettingHelper::RegisterSettingDisplayOffTimeObserver(SettingObserver::UpdateFunc& func)
51 {
52     SettingProvider& provider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
53     auto observer = provider.CreateObserver(SETTING_DISPLAY_OFF_TIME_KEY, func);
54     ErrCode ret = provider.RegisterObserver(observer);
55     if (ret != ERR_OK) {
56         POWER_HILOGW(COMP_UTILS, "register setting brightness observer failed, ret=%{public}d", ret);
57         return nullptr;
58     }
59     return observer;
60 }
61 
UnregisterSettingDisplayOffTimeObserver(sptr<SettingObserver> & observer)62 void SettingHelper::UnregisterSettingDisplayOffTimeObserver(sptr<SettingObserver>& observer)
63 {
64     if (observer == nullptr) {
65         return;
66     }
67     SettingProvider& provider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
68     provider.UnregisterObserver(observer);
69 }
70 
IsAutoAdjustBrightnessSettingValid()71 bool SettingHelper::IsAutoAdjustBrightnessSettingValid()
72 {
73     return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(SETTING_AUTO_ADJUST_BRIGHTNESS_KEY);
74 }
75 
SetSettingAutoAdjustBrightness(SwitchStatus status)76 void SettingHelper::SetSettingAutoAdjustBrightness(SwitchStatus status)
77 {
78     SettingProvider& provider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
79     ErrCode ret = provider.PutIntValue(SETTING_AUTO_ADJUST_BRIGHTNESS_KEY, static_cast<int32_t>(status));
80     if (ret != ERR_OK) {
81         POWER_HILOGW(
82             COMP_UTILS, "set setting auto adjust brightness failed, status=%{public}d, ret=%{public}d", status, ret);
83     }
84 }
85 
IsBrightnessSettingValid()86 bool SettingHelper::IsBrightnessSettingValid()
87 {
88     return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(SETTING_BRIGHTNESS_KEY);
89 }
90 
SetSettingBrightness(int32_t brightness)91 void SettingHelper::SetSettingBrightness(int32_t brightness)
92 {
93     SettingProvider& provider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
94     ErrCode ret = provider.PutIntValue(SETTING_BRIGHTNESS_KEY, brightness);
95     if (ret != ERR_OK) {
96         POWER_HILOGW(
97             COMP_UTILS, "set setting brightness failed, brightness=%{public}d, ret=%{public}d", brightness, ret);
98     }
99 }
100 
IsVibrationSettingValid()101 bool SettingHelper::IsVibrationSettingValid()
102 {
103     return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(SETTING_VIBRATION_KEY);
104 }
105 
SetSettingVibration(SwitchStatus status)106 void SettingHelper::SetSettingVibration(SwitchStatus status)
107 {
108     SettingProvider& provider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
109     ErrCode ret = provider.PutIntValue(SETTING_VIBRATION_KEY, static_cast<int32_t>(status));
110     if (ret != ERR_OK) {
111         POWER_HILOGW(COMP_UTILS, "set setting vibration failed, status=%{public}d, ret=%{public}d", status, ret);
112     }
113 }
114 
IsWindowRotationSettingValid()115 bool SettingHelper::IsWindowRotationSettingValid()
116 {
117     return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(SETTING_WINDOW_ROTATION_KEY);
118 }
119 
SetSettingWindowRotation(SwitchStatus status)120 void SettingHelper::SetSettingWindowRotation(SwitchStatus status)
121 {
122     SettingProvider& provider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
123     ErrCode ret = provider.PutIntValue(SETTING_WINDOW_ROTATION_KEY, static_cast<int32_t>(status));
124     if (ret != ERR_OK) {
125         POWER_HILOGW(COMP_UTILS, "set setting window rotation failed, status=%{public}d, ret=%{public}d", status, ret);
126     }
127 }
128 
RegisterSettingSuspendSourcesObserver(SettingObserver::UpdateFunc & func)129 sptr<SettingObserver> SettingHelper::RegisterSettingSuspendSourcesObserver(SettingObserver::UpdateFunc& func)
130 {
131     SettingProvider& provider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
132     auto observer = provider.CreateObserver(SETTING_POWER_SUSPEND_SOURCES_KEY, func);
133     ErrCode ret = provider.RegisterObserver(observer);
134     if (ret != ERR_OK) {
135         POWER_HILOGW(COMP_UTILS, "register setting brightness observer failed, ret=%{public}d", ret);
136         return nullptr;
137     }
138     return observer;
139 }
140 
UnregisterSettingSuspendSourcesObserver(sptr<SettingObserver> & observer)141 void SettingHelper::UnregisterSettingSuspendSourcesObserver(sptr<SettingObserver>& observer)
142 {
143     if (observer == nullptr) {
144         return;
145     }
146     SettingProvider& provider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
147     provider.UnregisterObserver(observer);
148 }
149 
IsSuspendSourcesSettingValid()150 bool SettingHelper::IsSuspendSourcesSettingValid()
151 {
152     return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(SETTING_POWER_SUSPEND_SOURCES_KEY);
153 }
154 
GetSettingSuspendSources()155 const std::string SettingHelper::GetSettingSuspendSources()
156 {
157     SettingProvider& provider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
158     std::string value;
159     ErrCode ret = provider.GetStringValue(SETTING_POWER_SUSPEND_SOURCES_KEY, value);
160     if (ret != ERR_OK) {
161         POWER_HILOGE(COMP_UTILS, "get setting power suspend sources key failed, ret=%{public}d", ret);
162     }
163     return value;
164 }
165 
SetSettingSuspendSources(const std::string & jsonConfig)166 void SettingHelper::SetSettingSuspendSources(const std::string& jsonConfig)
167 {
168     SettingProvider& provider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
169     ErrCode ret = provider.PutStringValue(SETTING_POWER_SUSPEND_SOURCES_KEY, jsonConfig);
170     if (ret != ERR_OK) {
171         POWER_HILOGE(COMP_UTILS, "set setting power suspend sources key failed, jsonConfig=%{public}s ret=%{public}d",
172             jsonConfig.c_str(), ret);
173     }
174 }
175 
RegisterSettingWakeupSourcesObserver(SettingObserver::UpdateFunc & func)176 sptr<SettingObserver> SettingHelper::RegisterSettingWakeupSourcesObserver(SettingObserver::UpdateFunc& func)
177 {
178     SettingProvider& provider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
179     auto observer = provider.CreateObserver(SETTING_POWER_WAKEUP_SOURCES_KEY, func);
180     ErrCode ret = provider.RegisterObserver(observer);
181     if (ret != ERR_OK) {
182         POWER_HILOGE(COMP_UTILS, "register setting brightness observer failed, ret=%{public}d", ret);
183         return nullptr;
184     }
185     return observer;
186 }
187 
UnregisterSettingWakeupSourcesObserver(sptr<SettingObserver> & observer)188 void SettingHelper::UnregisterSettingWakeupSourcesObserver(sptr<SettingObserver>& observer)
189 {
190     if (observer == nullptr) {
191         return;
192     }
193     SettingProvider& provider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
194     provider.UnregisterObserver(observer);
195 }
196 
IsWakeupSourcesSettingValid()197 bool SettingHelper::IsWakeupSourcesSettingValid()
198 {
199     return SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID).IsValidKey(SETTING_POWER_WAKEUP_SOURCES_KEY);
200 }
201 
GetSettingWakeupSources()202 const std::string SettingHelper::GetSettingWakeupSources()
203 {
204     SettingProvider& provider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
205     std::string value;
206     ErrCode ret = provider.GetStringValue(SETTING_POWER_WAKEUP_SOURCES_KEY, value);
207     if (ret != ERR_OK) {
208         POWER_HILOGE(COMP_UTILS, "get setting power Wakeup sources key failed, ret=%{public}d", ret);
209     }
210     return value;
211 }
212 
SetSettingWakeupSources(const std::string & jsonConfig)213 void SettingHelper::SetSettingWakeupSources(const std::string& jsonConfig)
214 {
215     SettingProvider& provider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
216     ErrCode ret = provider.PutStringValue(SETTING_POWER_WAKEUP_SOURCES_KEY, jsonConfig);
217     if (ret != ERR_OK) {
218         POWER_HILOGE(COMP_UTILS, "set setting power Wakeup sources key failed, jsonConfig=%{public}s ret=%{public}d",
219             jsonConfig.c_str(), ret);
220     }
221 }
222 } // namespace PowerMgr
223 } // namespace OHOS
224