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