196279301Sopenharmony_ci/*
296279301Sopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
396279301Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
496279301Sopenharmony_ci * you may not use this file except in compliance with the License.
596279301Sopenharmony_ci * You may obtain a copy of the License at
696279301Sopenharmony_ci *
796279301Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
896279301Sopenharmony_ci *
996279301Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1096279301Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1196279301Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1296279301Sopenharmony_ci * See the License for the specific language governing permissions and
1396279301Sopenharmony_ci * limitations under the License.
1496279301Sopenharmony_ci */
1596279301Sopenharmony_ci
1696279301Sopenharmony_ci#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_PREFERENCES_H
1796279301Sopenharmony_ci#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_PREFERENCES_H
1896279301Sopenharmony_ci
1996279301Sopenharmony_ci#include <memory>
2096279301Sopenharmony_ci#include <string>
2196279301Sopenharmony_ci
2296279301Sopenharmony_ci#include "singleton.h"
2396279301Sopenharmony_ci#include "refbase.h"
2496279301Sopenharmony_ci
2596279301Sopenharmony_ci#include "ans_inner_errors.h"
2696279301Sopenharmony_ci#include "distributed_preferences_database.h"
2796279301Sopenharmony_ci#include "distributed_preferences_info.h"
2896279301Sopenharmony_ci#include "notification_bundle_option.h"
2996279301Sopenharmony_ci
3096279301Sopenharmony_cinamespace OHOS {
3196279301Sopenharmony_cinamespace Notification {
3296279301Sopenharmony_ciclass DistributedPreferences : public DelayedSingleton<DistributedPreferences> {
3396279301Sopenharmony_cipublic:
3496279301Sopenharmony_ci    /**
3596279301Sopenharmony_ci     * @brief Set whether the device supports distributed notifications.
3696279301Sopenharmony_ci     *
3796279301Sopenharmony_ci     * @param isEnable Specifies whether to enable the device to support distributed notification.
3896279301Sopenharmony_ci     * @return Returns enable distributed result.
3996279301Sopenharmony_ci     */
4096279301Sopenharmony_ci    ErrCode SetDistributedEnable(bool isEnable);
4196279301Sopenharmony_ci
4296279301Sopenharmony_ci    /**
4396279301Sopenharmony_ci     * @brief Check if the device supports distributed notification.
4496279301Sopenharmony_ci     *
4596279301Sopenharmony_ci     * @param isEnable True if the device supports distributed notification; false otherwise.
4696279301Sopenharmony_ci     * @return Returns is distributed enabled result.
4796279301Sopenharmony_ci     */
4896279301Sopenharmony_ci    ErrCode GetDistributedEnable(bool &isEnable);
4996279301Sopenharmony_ci
5096279301Sopenharmony_ci    /**
5196279301Sopenharmony_ci     * @brief Set whether an application supports distributed notifications.
5296279301Sopenharmony_ci     *
5396279301Sopenharmony_ci     * @param bundleOption Indicates the bundle name and uid of an application.
5496279301Sopenharmony_ci     * @param isEnable Specifies whether to enable an application to support distributed notification.
5596279301Sopenharmony_ci     * @return Returns enable distributed by bundle result.
5696279301Sopenharmony_ci     */
5796279301Sopenharmony_ci    ErrCode SetDistributedBundleEnable(const sptr<NotificationBundleOption> &bundleOption, bool isEnable);
5896279301Sopenharmony_ci
5996279301Sopenharmony_ci    /**
6096279301Sopenharmony_ci     * @brief Check whether an application supports distributed notifications.
6196279301Sopenharmony_ci     *
6296279301Sopenharmony_ci     * @param bundleOption Indicates the bundle name and uid of an application.
6396279301Sopenharmony_ci     * @param isEnable True if the application supports distributed notification; false otherwise.
6496279301Sopenharmony_ci     * @return Returns is distributed enabled by bundle result.
6596279301Sopenharmony_ci     */
6696279301Sopenharmony_ci    ErrCode GetDistributedBundleEnable(const sptr<NotificationBundleOption> &bundleOption, bool &isEnable);
6796279301Sopenharmony_ci
6896279301Sopenharmony_ci    /**
6996279301Sopenharmony_ci     * @brief Remove the setting of whether the application supports distributed notification.
7096279301Sopenharmony_ci     *
7196279301Sopenharmony_ci     * @param bundleOption Indicates the bundle name and uid of an application.
7296279301Sopenharmony_ci     * @return Returns remove the setting result.
7396279301Sopenharmony_ci     */
7496279301Sopenharmony_ci    ErrCode DeleteDistributedBundleInfo(const sptr<NotificationBundleOption> &bundleOption);
7596279301Sopenharmony_ci
7696279301Sopenharmony_ci    /**
7796279301Sopenharmony_ci     * @brief Remove all distributed enabled setting info from DB.
7896279301Sopenharmony_ci     *
7996279301Sopenharmony_ci     * @return Returns remove the setting result.
8096279301Sopenharmony_ci     */
8196279301Sopenharmony_ci    ErrCode ClearDataInRestoreFactorySettings();
8296279301Sopenharmony_ci
8396279301Sopenharmony_ci    /**
8496279301Sopenharmony_ci     * @brief Set whether to sync notifications to devices that do not have the app installed.
8596279301Sopenharmony_ci     *
8696279301Sopenharmony_ci     * @param userId Indicates the specific user.
8796279301Sopenharmony_ci     * @param enabled Allow or disallow sync notifications.
8896279301Sopenharmony_ci     * @return Returns set enabled result.
8996279301Sopenharmony_ci     */
9096279301Sopenharmony_ci    ErrCode SetSyncEnabledWithoutApp(const int32_t userId, const bool enabled);
9196279301Sopenharmony_ci    ErrCode GetSyncEnabledWithoutApp(const int32_t userId, bool &enabled);
9296279301Sopenharmony_ci
9396279301Sopenharmony_ciprivate:
9496279301Sopenharmony_ci    bool InitDistributedAllInfo();
9596279301Sopenharmony_ci    void GetDistributedMainKey(std::string &key);
9696279301Sopenharmony_ci    void GetDistributedBundleKey(const sptr<NotificationBundleOption> &bundleOption, std::string &key);
9796279301Sopenharmony_ci    bool ResolveDistributedKey(const DistributedKv::Entry &entry);
9896279301Sopenharmony_ci    void GetEnabledWithoutApp(const int32_t userId, std::string &key);
9996279301Sopenharmony_ci    bool ResolveDistributedEnable(const std::string &value);
10096279301Sopenharmony_ci    bool ResolveDistributedBundleEnable(const std::string &key, const int32_t startPos, const std::string &value);
10196279301Sopenharmony_ci    bool ResolveSyncWithoutAppEnable(const std::string &key, const int32_t startPos, const std::string &value);
10296279301Sopenharmony_ci
10396279301Sopenharmony_ci    std::unique_ptr<DistributedPreferencesInfo> preferencesInfo_ = nullptr;
10496279301Sopenharmony_ci    std::unique_ptr<DistributedPreferencesDatabase> database_ = nullptr;
10596279301Sopenharmony_ci
10696279301Sopenharmony_ci    DECLARE_DELAYED_SINGLETON(DistributedPreferences);
10796279301Sopenharmony_ci    DISALLOW_COPY_AND_MOVE(DistributedPreferences);
10896279301Sopenharmony_ci};
10996279301Sopenharmony_ci}  // namespace Notification
11096279301Sopenharmony_ci}  // namespace OHOS
11196279301Sopenharmony_ci#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_DISTRIBUTED_PREFERENCES_H
112