1/*
2 * Copyright (c) 2022-2023 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 BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_PREFERENCES_H
17#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_PREFERENCES_H
18
19#include <memory>
20#include <string>
21
22#include "singleton.h"
23#include "refbase.h"
24
25#include "ans_inner_errors.h"
26#include "distributed_preferences_database.h"
27#include "distributed_preferences_info.h"
28#include "notification_bundle_option.h"
29
30namespace OHOS {
31namespace Notification {
32class DistributedPreferences : public DelayedSingleton<DistributedPreferences> {
33public:
34    /**
35     * @brief Set whether the device supports distributed notifications.
36     *
37     * @param isEnable Specifies whether to enable the device to support distributed notification.
38     * @return Returns enable distributed result.
39     */
40    ErrCode SetDistributedEnable(bool isEnable);
41
42    /**
43     * @brief Check if the device supports distributed notification.
44     *
45     * @param isEnable True if the device supports distributed notification; false otherwise.
46     * @return Returns is distributed enabled result.
47     */
48    ErrCode GetDistributedEnable(bool &isEnable);
49
50    /**
51     * @brief Set whether an application supports distributed notifications.
52     *
53     * @param bundleOption Indicates the bundle name and uid of an application.
54     * @param isEnable Specifies whether to enable an application to support distributed notification.
55     * @return Returns enable distributed by bundle result.
56     */
57    ErrCode SetDistributedBundleEnable(const sptr<NotificationBundleOption> &bundleOption, bool isEnable);
58
59    /**
60     * @brief Check whether an application supports distributed notifications.
61     *
62     * @param bundleOption Indicates the bundle name and uid of an application.
63     * @param isEnable True if the application supports distributed notification; false otherwise.
64     * @return Returns is distributed enabled by bundle result.
65     */
66    ErrCode GetDistributedBundleEnable(const sptr<NotificationBundleOption> &bundleOption, bool &isEnable);
67
68    /**
69     * @brief Remove the setting of whether the application supports distributed notification.
70     *
71     * @param bundleOption Indicates the bundle name and uid of an application.
72     * @return Returns remove the setting result.
73     */
74    ErrCode DeleteDistributedBundleInfo(const sptr<NotificationBundleOption> &bundleOption);
75
76    /**
77     * @brief Remove all distributed enabled setting info from DB.
78     *
79     * @return Returns remove the setting result.
80     */
81    ErrCode ClearDataInRestoreFactorySettings();
82
83    /**
84     * @brief Set whether to sync notifications to devices that do not have the app installed.
85     *
86     * @param userId Indicates the specific user.
87     * @param enabled Allow or disallow sync notifications.
88     * @return Returns set enabled result.
89     */
90    ErrCode SetSyncEnabledWithoutApp(const int32_t userId, const bool enabled);
91    ErrCode GetSyncEnabledWithoutApp(const int32_t userId, bool &enabled);
92
93private:
94    bool InitDistributedAllInfo();
95    void GetDistributedMainKey(std::string &key);
96    void GetDistributedBundleKey(const sptr<NotificationBundleOption> &bundleOption, std::string &key);
97    bool ResolveDistributedKey(const DistributedKv::Entry &entry);
98    void GetEnabledWithoutApp(const int32_t userId, std::string &key);
99    bool ResolveDistributedEnable(const std::string &value);
100    bool ResolveDistributedBundleEnable(const std::string &key, const int32_t startPos, const std::string &value);
101    bool ResolveSyncWithoutAppEnable(const std::string &key, const int32_t startPos, const std::string &value);
102
103    std::unique_ptr<DistributedPreferencesInfo> preferencesInfo_ = nullptr;
104    std::unique_ptr<DistributedPreferencesDatabase> database_ = nullptr;
105
106    DECLARE_DELAYED_SINGLETON(DistributedPreferences);
107    DISALLOW_COPY_AND_MOVE(DistributedPreferences);
108};
109}  // namespace Notification
110}  // namespace OHOS
111#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_PREFERENCES_H
112