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 #ifndef POWERMGR_POWER_MANAGER_POWER_SETTING_HELPER_H
17 #define POWERMGR_POWER_MANAGER_POWER_SETTING_HELPER_H
18 
19 #include "datashare_helper.h"
20 #include "errors.h"
21 #include "mutex"
22 #include "setting_observer.h"
23 
24 namespace OHOS {
25 namespace PowerMgr {
26 class SettingProvider : public NoCopyable {
27 public:
28     static SettingProvider& GetInstance(int32_t systemAbilityId);
29     ErrCode GetStringValue(const std::string& key, std::string& value);
30     ErrCode GetIntValue(const std::string& key, int32_t& value);
31     ErrCode GetLongValue(const std::string& key, int64_t& value);
32     ErrCode GetBoolValue(const std::string& key, bool& value);
33     ErrCode PutStringValue(const std::string& key, const std::string& value, bool needNotify = true);
34     ErrCode PutIntValue(const std::string& key, int32_t value, bool needNotify = true);
35     ErrCode PutLongValue(const std::string& key, int64_t value, bool needNotify = true);
36     ErrCode PutBoolValue(const std::string& key, bool value, bool needNotify = true);
37     bool IsValidKey(const std::string& key);
38     sptr<SettingObserver> CreateObserver(const std::string& key, SettingObserver::UpdateFunc& func);
39     static void ExecRegisterCb(const sptr<SettingObserver>& observer);
40     ErrCode RegisterObserver(const sptr<SettingObserver>& observer);
41     ErrCode UnregisterObserver(const sptr<SettingObserver>& observer);
42     void UpdateCurrentUserId();
43     void CopyDataForUpdateScene();
44 
45 protected:
46     ~SettingProvider() override;
47 
48 private:
49 #ifdef POWER_MANAGER_ENABLE_CHARGING_TYPE_SETTING
50     // AC for Alternating Current, means charing supply
51     // DC for Direct Current, means battery supply
52     static constexpr const char* SETTING_DISPLAY_AC_OFF_TIME_KEY {"settings.display.ac.screen_off_timeout"};
53     static constexpr const char* SETTING_DISPLAY_DC_OFF_TIME_KEY {"settings.display.dc.screen_off_timeout"};
54     static constexpr const char* SETTING_POWER_AC_SUSPEND_SOURCES_KEY {"settings.power.ac.suspend_sources"};
55     static constexpr const char* SETTING_POWER_DC_SUSPEND_SOURCES_KEY {"settings.power.dc.suspend_sources"};
56 #else
57     static constexpr const char* SETTING_DISPLAY_OFF_TIME_KEY {"settings.display.screen_off_timeout"};
58     static constexpr const char* SETTING_POWER_SUSPEND_SOURCES_KEY {"settings.power.suspend_sources"};
59 #endif
60     static constexpr const char* SETTING_AUTO_ADJUST_BRIGHTNESS_KEY {"settings.display.auto_screen_brightness"};
61     static constexpr const char* SETTING_BRIGHTNESS_KEY {"settings.display.screen_brightness_status"};
62     static constexpr const char* SETTING_VIBRATION_KEY {"physic_navi_haptic_feedback_enabled"};
63     static constexpr const char* SETTING_WINDOW_ROTATION_KEY {"settings.general.accelerometer_rotation_status"};
64     static constexpr const char* SETTING_POWER_WAKEUP_SOURCES_KEY {"settings.power.wakeup_sources"};
65     static constexpr const char* SETTING_INTELL_VOICE_KEY {"intell_voice_trigger_enabled"};
66     static constexpr const char* SETTING_POWER_WAKEUP_DOUBLE_KEY {"settings.power.wakeup_double_click"};
67     static constexpr const char* SETTING_POWER_WAKEUP_PICKUP_KEY {"settings.power.wakeup_pick_up"};
68     static constexpr const char* SETTING_POWER_MODE_KEY  {"settings.power.smart_mode_status"};
69     static constexpr const char* SETTING_POWER_MODE_BACKUP_KEY  {"settings.power.smart_mode_status.backup"};
70     static constexpr const char* SETTING_POWER_WAKEUP_LID_KEY {"settings.power.wakeup_lid"};
71     static SettingProvider* instance_;
72     static std::mutex settingMutex_;
73     static sptr<IRemoteObject> remoteObj_;
74     static int32_t currentUserId_;
75 
76     static void Initialize(int32_t systemAbilityId);
77     static std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(const std::string& key);
78     static bool ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper>& helper);
79     static Uri AssembleUri(const std::string& key);
80     static bool IsNeedMultiUser(const std::string& key);
81     static std::string ReplaceUserIdForUri(int32_t userId);
82     bool IsNeedDataMigrationCopy();
83     void DataMigrationCopy();
84     ErrCode GetStringValueGlobal(const std::string& key, std::string& value);
85     bool IsValidKeyGlobal(const std::string& key);
86 };
87 } // namespace PowerMgr
88 } // namespace OHOS
89 #endif // POWERMGR_POWER_MANAGER_POWER_SETTING_HELPER_H
90