1 /* 2 * Copyright (C) 2022-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 NETWORK_SEARCH_SETTINGS_RDB_HELPER_H 17 #define NETWORK_SEARCH_SETTINGS_RDB_HELPER_H 18 19 #include <singleton.h> 20 #include <memory> 21 #include <utility> 22 23 #include "datashare_helper.h" 24 #include "datashare_predicates.h" 25 #include "datashare_result_set.h" 26 #include "datashare_values_bucket.h" 27 #include "data_ability_observer_stub.h" 28 #include "iservice_registry.h" 29 #include "result_set.h" 30 #include "system_ability_definition.h" 31 #include "uri.h" 32 #include "system_ability_status_change_stub.h" 33 #include "common_event_subscriber.h" 34 #include <ffrt.h> 35 36 #include "network_search_handler.h" 37 38 namespace OHOS { 39 namespace Telephony { 40 class SettingUtils : public DelayedSingleton<SettingUtils> { 41 DECLARE_DELAYED_SINGLETON(SettingUtils); 42 43 public: 44 static const std::string NETWORK_SEARCH_SETTING_URI; 45 static const std::string NETWORK_SEARCH_SETTING_AUTO_TIME_URI; 46 static const std::string NETWORK_SEARCH_SETTING_AUTO_TIMEZONE_URI; 47 static const std::string NETWORK_SEARCH_SETTING_AIRPLANE_MODE_URI; 48 static const std::string NETWORK_SEARCH_SETTING_PREFERRED_NETWORK_MODE_URI; 49 static const std::string SETTINGS_NETWORK_SEARCH_AUTO_TIME; 50 static const std::string SETTINGS_NETWORK_SEARCH_AUTO_TIMEZONE; 51 static const std::string SETTINGS_NETWORK_SEARCH_AIRPLANE_MODE; 52 static const std::string SETTINGS_NETWORK_SEARCH_PREFERRED_NETWORK_MODE; 53 static const std::string COMMON_EVENT_DATA_SHARE_READY; 54 55 bool RegisterSettingsObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver); 56 bool UnRegisterSettingsObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver); 57 void RegisterSettingsObserver(); 58 void SetCommonEventSubscribeInfo(const EventFwk::CommonEventSubscribeInfo &subscribeInfo); 59 std::shared_ptr<EventFwk::CommonEventSubscriber> GetCommonEventSubscriber(); 60 int32_t Query(Uri uri, const std::string &key, std::string &value); 61 int32_t Insert(Uri uri, const std::string &key, const std::string &value); 62 int32_t Update(Uri uri, const std::string &key, const std::string &value); 63 void UpdateDdsState(bool isReady); 64 65 private: 66 std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(); 67 std::shared_ptr<DataShare::DataShareHelper> CreateNonBlockDataShareHelper(); 68 bool RegisterToDataShare(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver); 69 70 private: 71 const std::string SETTING_KEY = "KEYWORD"; 72 const std::string SETTING_VALUE = "VALUE"; 73 const int32_t RDB_INVALID_VALUE = -1; 74 ffrt::mutex mtx_; 75 std::vector<std::pair<Uri, sptr<AAFwk::IDataAbilityObserver>>> registerInfos_; 76 std::shared_ptr<EventFwk::CommonEventSubscriber> commonEventSubscriber_ = nullptr; 77 std::atomic<bool> isDdsReady_ = false; 78 79 private: 80 class BroadcastSubscriber : public EventFwk::CommonEventSubscriber { 81 public: 82 BroadcastSubscriber(const EventFwk::CommonEventSubscribeInfo &sp); 83 ~BroadcastSubscriber() = default; 84 void OnReceiveEvent(const EventFwk::CommonEventData &data) override; 85 }; 86 }; 87 88 class SettingEventCode { 89 public: 90 static const int32_t BASE = 0x00050000; 91 static const int32_t MSG_AUTO_TIME = BASE + 0; 92 static const int32_t MSG_AUTO_TIMEZONE = BASE + 1; 93 static const int32_t MSG_AUTO_AIRPLANE_MODE = BASE + 2; 94 }; 95 96 class AutoTimeObserver : public AAFwk::DataAbilityObserverStub { 97 public: 98 explicit AutoTimeObserver(std::shared_ptr<NetworkSearchHandler> &networkSearchHandler); 99 ~AutoTimeObserver() = default; 100 void OnChange() override; 101 102 private: 103 std::shared_ptr<NetworkSearchHandler> networkSearchHandler_; 104 }; 105 106 class AutoTimezoneObserver : public AAFwk::DataAbilityObserverStub { 107 public: 108 explicit AutoTimezoneObserver(std::shared_ptr<NetworkSearchHandler> &networkSearchHandler); 109 ~AutoTimezoneObserver() = default; 110 void OnChange() override; 111 112 private: 113 std::shared_ptr<NetworkSearchHandler> networkSearchHandler_; 114 }; 115 116 class AirplaneModeObserver : public AAFwk::DataAbilityObserverStub { 117 public: 118 explicit AirplaneModeObserver(std::shared_ptr<NetworkSearchHandler> &networkSearchHandler); 119 ~AirplaneModeObserver() = default; 120 void OnChange() override; 121 122 private: 123 std::shared_ptr<NetworkSearchHandler> networkSearchHandler_; 124 }; 125 } // namespace Telephony 126 } // namespace OHOS 127 #endif // NETWORK_SEARCH_SETTINGS_RDB_HELPER_H 128