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