• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 OHOS_ROSEN_SETTING_PROVIDER_H
17 #define OHOS_ROSEN_SETTING_PROVIDER_H
18 
19 #include "datashare_helper.h"
20 #include "errors.h"
21 #include "mutex"
22 #include "setting_observer.h"
23 #include <refbase.h>
24 
25 namespace OHOS {
26 namespace Rosen {
27 class SettingProvider : public NoCopyable, public RefBase {
28 public:
29     static SettingProvider& GetInstance(int32_t systemAbilityId);
30     ErrCode GetStringValue(const std::string& key, std::string& value);
31     ErrCode GetStringValueMultiUser(const std::string& key, std::string& value);
32     ErrCode GetIntValue(const std::string& key, int32_t& value);
33     ErrCode GetLongValue(const std::string& key, int64_t& value);
34     ErrCode GetBoolValue(const std::string& key, bool& value);
35     ErrCode PutStringValue(const std::string& key, const std::string& value, bool needNotify = true);
36     ErrCode PutIntValue(const std::string& key, int32_t value, bool needNotify = true);
37     ErrCode PutLongValue(const std::string& key, int64_t value, bool needNotify = true);
38     ErrCode PutBoolValue(const std::string& key, bool value, bool needNotify = true);
39     bool IsValidKey(const std::string& key);
40     sptr<SettingObserver> CreateObserver(const std::string& key, SettingObserver::UpdateFunc& func);
41     static void ExecRegisterCb(const sptr<SettingObserver>& observer);
42     ErrCode RegisterObserver(const sptr<SettingObserver>& observer);
43     ErrCode UnregisterObserver(const sptr<SettingObserver>& observer);
44 
45     ErrCode RegisterObserverByTable(const sptr<SettingObserver>& observer, std::string tableName);
46     ErrCode UnregisterObserverByTable(const sptr<SettingObserver>& observer, std::string tableName);
47     ErrCode GetIntValueMultiUserByTable(const std::string& key, int32_t& value, std::string tableName);
48     ErrCode GetLongValueMultiUserByTable(const std::string& key, int64_t& value, std::string tableName);
49     ErrCode GetStringValueMultiUserByTable(const std::string& key, std::string& value, std::string tableName);
50 
51     ~SettingProvider() = default;
52 
53 private:
54     static sptr<SettingProvider> instance_;
55     static std::mutex instanceMutex_;
56     static sptr<IRemoteObject> remoteObj_;
57 
58     static void Initialize(int32_t systemAbilityId);
59     static std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper();
60     static std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelperMultiUser();
61     static bool ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper>& helper);
62     static Uri AssembleUri(const std::string& key);
63     static Uri AssembleUriMultiUser(const std::string& key);
64     static Uri AssembleUriWallMultiUser(const std::string& key);
65 
66     static std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelperMultiUserByTable(std::string tableName);
67     static Uri AssembleUriMultiUserByTable(const std::string& key, std::string tableName);
68 };
69 } // namespace Rosen
70 } // namespace OHOS
71 #endif // OHOS_ROSEN_SETTING_PROVIDER_H
72