• 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 <cinttypes>
19 #include <system_ability_definition.h>
20 #include "power_log.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()29 int64_t SettingHelper::GetSettingDisplayOffTime()
30 {
31     SettingProvider& provider = SettingProvider::GetInstance(POWER_MANAGER_SERVICE_ID);
32     int64_t value;
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 } // namespace PowerMgr
129 } // namespace OHOS
130