1 /*
2  * Copyright (C) 2021-2023 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 OHOS_WIFI_DATASHARE_UTILS_H
17 #define OHOS_WIFI_DATASHARE_UTILS_H
18 
19 #include <memory>
20 #include <utility>
21 #include <singleton.h>
22 
23 #include "iremote_object.h"
24 #include "datashare_helper.h"
25 #include "datashare_predicates.h"
26 #include "datashare_result_set.h"
27 #include "iservice_registry.h"
28 #include "uri.h"
29 #include "wifi_errcode.h"
30 
31 namespace OHOS {
32 namespace Wifi {
33 namespace {
34 constexpr const char *SETTINGS_DATASHARE_URL_AIRPLANE_MODE =
35     "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=airplane_mode";
36 constexpr const char *SETTINGS_DATASHARE_KEY_AIRPLANE_MODE = "settings.telephony.airplanemode";
37 constexpr const char *SETTINGS_DATASHARE_KEY_LOCATION_MODE = "location_switch_enable";
38 
39 #ifndef OHOS_ARCH_LITE
40 constexpr const char *SETTINGS_DATASHARE_URI_WIFI_ON =
41     "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=wifi_on";
42 constexpr const char *SETTINGS_DATASHARE_KEY_WIFI_ON = "wifi_on";
43 #endif
44 constexpr const char *SETTINGS_DATASHARE_URI_WIFI_ALLOW_SEMI_ACTIVE =
45     "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=";
46 constexpr const char *SETTINGS_DATASHARE_KEY_WIFI_ALLOW_SEMI_ACTIVE =
47     "settings.collaboration.multi_device_collaboration_service_switch";
48 }
49 
50 class WifiDataShareHelperUtils : DelayedSingleton<WifiDataShareHelperUtils> {
51 public:
52     /**
53      * @Description : WifiDataShareHelperUtils constructor
54      */
55     WifiDataShareHelperUtils() = default;
56 
57     /**
58      * @Description : WifiDataShareHelperUtils destructor
59      */
60     ~WifiDataShareHelperUtils() = default;
61 
62     /**
63      * @Description : Check if SettingsData ready
64      *
65      * @return true - datashare ready
66      */
67     bool CheckIfSettingsDataReady();
68 
69     /**
70      * @Description : Query function
71      *
72      * @param uri - Querying the database path
73      * @param key - key
74      * @param value - value
75      * @param onlySettingsData - false
76      * @return query error code
77      */
78     ErrCode Query(Uri &uri, const std::string &key, std::string &value, bool onlySettingsData = false);
79 
80     /**
81      * @Description : Insert function
82      *
83      * @param uri - Inserting the database path
84      * @param key - key
85      * @param value - value
86      * @return Insert error code
87      */
88     ErrCode Insert(Uri &uri, const std::string &key, const std::string &value);
89 
90     /**
91      * @Description : Update function
92      *
93      * @param uri - Updateing the database path
94      * @param key - key
95      * @param value - value
96      * @return Update error code
97      */
98     ErrCode Update(Uri &uri, const std::string &key, const std::string &value);
99 
100     /**
101      * @Description : Register observer function
102      *
103      * @param uri - Register observer the database path
104      * @param key - key
105      * @param value - value
106      * @return Register observer error code
107      */
108     ErrCode RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &observer);
109 
110     /**
111      * @Description : Unregister observer function
112      *
113      * @param uri - Register observer the database path
114      * @param key - key
115      * @param value - value
116      * @return Unregister observer error code
117      */
118     ErrCode UnRegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &observer);
119 
120     std::string GetLoactionDataShareUri();
121 
122 private:
123     std::shared_ptr<DataShare::DataShareHelper> WifiCreateDataShareHelper(bool onlySettingsData = false);
124     void ClearResources(std::shared_ptr<DataShare::DataShareHelper> operatrPtr,
125         std::shared_ptr<DataShare::DataShareResultSet> result);
126     bool IsDataMgrServiceActive();
127 
128     bool isDataShareReady_ = false;
129 };
130 
131 class IWifiDataShareRemoteBroker : public IRemoteBroker {
132 public:
133     DECLARE_INTERFACE_DESCRIPTOR(u"ohos.wifi.IWifiScan");
134 };
135 
136 }   // namespace Wifi
137 }   // namespace OHOS
138 #endif
139