1 /* 2 * Copyright (c) 2024-2024 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 ACCESSIBILITY_DATASHARE_HELPER 17 #define ACCESSIBILITY_DATASHARE_HELPER 18 19 #include "accessibility_setting_observer.h" 20 #include "accessibility_def.h" 21 22 #ifdef OHOS_BUILD_ENABLE_DATA_SHARE 23 #include "datashare_helper.h" 24 #endif 25 26 namespace OHOS { 27 namespace Accessibility { 28 29 enum DATASHARE_TYPE : int32_t { 30 GLOBAL, 31 SYSTEM, 32 SECURE, 33 }; 34 35 class AccessibilityDatashareHelper { 36 public: 37 AccessibilityDatashareHelper(DATASHARE_TYPE type, int32_t accountID); 38 ~AccessibilityDatashareHelper(); 39 std::string GetStringValue(const std::string& key, const std::string& defaultValue); 40 int32_t GetIntValue(const std::string& key, const int32_t& defaultValue); 41 int64_t GetLongValue(const std::string& key, const int64_t& defaultValue); 42 bool GetBoolValue(const std::string& key, const bool& defaultValue); 43 float GetFloatValue(const std::string& key, const float& defaultValue); 44 45 RetError PutStringValue(const std::string& key, const std::string& value, bool needNotify = true); 46 RetError PutIntValue(const std::string& key, int32_t value, bool needNotify = true); 47 RetError PutLongValue(const std::string& key, int64_t value, bool needNotify = true); 48 RetError PutBoolValue(const std::string& key, bool value, bool needNotify = true); 49 RetError PutFloatValue(const std::string& key, float value, bool needNotify = true); 50 51 void Initialize(int32_t systemAbilityId); 52 53 sptr<AccessibilitySettingObserver> CreateObserver(const std::string& key, 54 AccessibilitySettingObserver::UpdateFunc& func); 55 RetError RegisterObserver(const sptr<AccessibilitySettingObserver>& observer); 56 RetError UnregisterObserver(const sptr<AccessibilitySettingObserver>& observer); 57 58 RetError RegisterObserver(const std::string& key, AccessibilitySettingObserver::UpdateFunc& func); 59 RetError UnregisterObserver(const std::string& key); 60 61 private: 62 #ifdef OHOS_BUILD_ENABLE_DATA_SHARE 63 std::shared_ptr<DataShare::DataShareHelper> CreateDatashareHelper(); 64 bool DestoryDatashareHelper(std::shared_ptr<DataShare::DataShareHelper>& helper); 65 #endif 66 Uri AssembleUri(const std::string& key); 67 68 private: 69 DATASHARE_TYPE type_; 70 int32_t accountId_; 71 std::string uriProxyStr_; 72 sptr<IRemoteObject> remoteObj_ = nullptr; 73 #ifdef OHOS_BUILD_ENABLE_DATA_SHARE 74 std::shared_ptr<DataShare::DataShareHelper> dataShareHelper_ = nullptr; 75 #endif 76 static std::mutex observerMutex_; 77 std::map<std::string, sptr<AccessibilitySettingObserver>> settingObserverMap_; 78 }; 79 } // namespace Accessibility 80 } // namespace OHOS 81 #endif // ACCESSIBILITY_DATASHARE_HELPER 82