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