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