• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 DUPDATE_NET_MANAGER_H
17 #define DUPDATE_NET_MANAGER_H
18 
19 #include "singleton.h"
20 
21 #include <set>
22 
23 #include "dupdate_inet_observer.h"
24 #include "dupdate_net_observer.h"
25 
26 namespace OHOS {
27 namespace UpdateEngine {
28 struct NetChangeCallback {
29     std::set<NetType> netTypes;
30     NetObserverCallback function;
31 };
32 
33 class NetManager final : public INetObserverCallback,
34                          public std::enable_shared_from_this<NetManager>,
35                          public DelayedSingleton<NetManager> {
36     DECLARE_DELAYED_SINGLETON(NetManager);
37 
38 public:
39     void Init();
40     NetType GetNetType();
41     bool IsNetAvailable(NetType netType = NetType::CELLULAR_AND_WIFI);
42 
43     bool RegisterCallback(
44         NetChangeCallbackType callbackType, const std::set<NetType> &netTypes, NetObserverCallback cb);
45     void UnregisterCallback(NetChangeCallbackType callbackType);
46 
47     bool OnNetChange(NetType netType) final;
48 
49 private:
50     bool IsBaseNetType(NetType netType);
51 
52     sptr<NetObserver> observer_ = nullptr;
53     std::mutex netChangeMutex_;
54     NetType netType_ = NetType::NO_NET;
55     std::map<NetChangeCallbackType, NetChangeCallback> netChangeCallbackMap_;
56 };
57 } // namespace UpdateEngine
58 } // namespace OHOS
59 #endif // DUPDATE_NET_MANAGER_H